lundi 5 janvier 2015

Which weak algorithmic strategies I have that lead me to failing coding this simple algorithm?


I am a graduate student in computer science who graduated recently from a famous university. I applied to my first software engineering job and I failed the technical interview by failing to answer the first extremely silly programming question. I wasted all my time on trying to solve this question and never touched the other questions, yet I failed to answer it. I want to know why I failed and which weak points in my problem-solving thinking I have that I need to address and solve.


First here is the question: given an input n that defines the number of columns, return a string that contains an X. A picture for what I mean:


enter image description here


Here is my stupid approach of solving it:



public static void main(String[] args) {


int size = 9;

for ( int i = 1; i <= size; i++) {

if ( i <= size/2 ) {
for ( int k = 1; k < i; k++ ) {
System.out.print(" ");
}

System.out.print("*");

for(int j = i; j < size-i; j++) {
System.out.print(" ");
}

System.out.print("*");
}
else if ( i == (size/2)+1 ) {
for ( int k = 1; k < i; k++ ) {
System.out.print(" ");
}
System.out.println( "*");

}
else {

// for ( int k = 0; k < (size/2) -1; k++) {
// System.out.print(" ");
// }
//
// System.out.print("*");
//
// for ( int j = i; j > size-i; j-- ) {
// System.out.print(" ");
// }
//
// System.out.print("*");

}

System.out.println();
}


As you can see, although I wasted 1 hour just trying to answer it, I haven't even completed the solution. The next day I went to two friends of mine and they solved the questions in 2 minutes by creating a 2D array of chars and using a single for-loop. That shocked me and made me feel that I am in fact a very stupid person. This actually affected me personally since having a graduate degree in computer science from a famous university and not being able to solve such a ridiculous question. I took too much time for my graduate studies so I became seriously thinking that I have a mental problem or something :/


But anyway I decided to go with a positive attitude and try to solve my problem. So based on the kind of that programming question and my solution to it, here are my questions to you:


1- Given the approach I used to solve the problem, am I fixable somehow regarding my problem-solving approaches?


2- If so, what kind of problem-solving/algorithmic category does that kind of question fall into? & how to improve?


I need to note that during my studies I had to code many complicated algorithms and software.





Aucun commentaire:

Enregistrer un commentaire