Asking Clarifying Questions in Job interviews has always been a recommended practice. Some of my friends were discussing their not-so-successful interview experiences and many times they mentioned that they solved the problem only to realize that
- Interviewer had asked some other question OR
- They had forgotten to clarify each and every case with the interviewer before attempting the solution etc.
I am not a guru in doing this but let us try this out.
When in an interview, before you jump to the solution, you should be absolutely sure that you and your interviewer are both thinking in the same direction. Interviewer asks you to solve a problem. If you dont’t ask clarifying questions, your interviewer will assume that you have fully understood the problem. So its always good to speak out what you are thinking and your interviewer will correct you thoughts are not aligned with his thoughts.
For example, Let us consider a simple (and famous question). I may not be able to identify all the cases (but who cares 🙂 .. after all its just another blog :)).
“You are getting a char* that represents a string. If this string represents a Palindrome, return TRUE, otherwise return FALSE”.
Now, lets us see how can we start solving it.
You don’t have TRUE and FALSE defined. Tell you interviewer that for simplicity, you are defining them as
#define TRUE 1
#define FALSE 0
Tell your interviewer that you could have considered using ENUM BOOL but you are focusing on the problem right now.
Define a prototype as
unsingned int IsPalindrome(char* str);
Also clarify that you are supposing that you will receive an 8 bit char string.
Discuss with your interviewer that you would do the following
if (NULL == str) return FALSE;
if (!(*str)) return FALSE; // Zero Length Strings
if (‘ ‘ != str && !(str)) return TRUE; // 1 Character strings (See next point for spaces. They might be considered palindrome or not. If they are considered palindromes, then you can omit ‘ ‘ != str )
Discuss with your interviewer the issue of SPACES.
If your interviewer says that you have to ignore spaces, then
Any string of length >=1 containing SPACES only will return FALSE
Strings like “bbaaa bb” will be considered palindrome and will return TRUE
If your interviewer says that you have to consider spaces, then
Any string of length >=1 containing SPACES only will return TRUE
Strings like “bbaaa bb” will not be considered palindrome and will return FALSE
Suggest your interviewer to change the prototype to
unsingned int IsPalindrome(char* str, size_t length);
So that you can avoid Buffer overruns. Your interviewer will either GUARANTEE that you will always get a NULL TERMINATED string (in order to simplify things) or your interviewer will allow to change the prototype. In latter case, you must change your logic accordingly.
Well, these were some questions that you can ask before solving a problem. After this, things are pretty much clear between you and your interviewer. You can carry on solving the problem after this.
Clarifying questions vary from problem to problem but there are always certain issues that you should clarify before you jump to the solution.
P.S. If someone reading this blog wants to add more questions regarding this problem, kindly do so in the comments section.