How to Answer Questions
Here are some tips for the process of handling interview questions:
- Ask questions to clarify what is being asked. Make sure you understand the question and relevant details clearly before trying to solve it!
- Discuss a possible Algorithm with the interviewer. Often you can start by finding a brute-force solution before moving to a better one. (Just make sure that the interviewer understands that this solution is just your first step.)
- Once you have your solution, start turning it into code. Find out from the interviewer how precise your syntax needs to be, sometimes you can even use pseudo-code. Use data structures and sub-procedures when possible to make your code more compact and demonstrate that you can break down a problem effectively.
- Review your code and try out some sample inputs on it. Check the edge cases to make sure everything works!
Sample Interview Session
Think about how you would respond to the interviewer before (clicking and) reading each response below.
Interviewer: You're given a large array of numbers. How would you find a specific number within it?
You: Without modifying anything, I could simply iterate through the array until the number is found.
Interviewer: Good, but you're going to need find many different numbers on a given list. Are there steps you could take beforehand so each number can be found quickly?
You: I can prepare the data beforehand by either sorting it or putting it into a HashSet.
Interviewer: OK, Sorting sounds good like a good approach. How would you find the numbers after that?
You: I could perform a binary search to find the number quickly.
Interviewer: OK, go ahead and code up the solution. Create a method that takes in an unsorted array and prepares the data, and then create another method that returns the location of a given number in the array.
You: Can I use built-in library functions to sort the array?
Interviewer: Sure, but you need to write your own code to find the numbers.
You: What should I do when the given number isn't found the array, should I return -1 in such a case?
Interviewer: Yes, that's a good idea.
You: Creates method that prepares the data with built-in sorting method. Next, carefully writes code for binary search.
Note: See Index Match to practice a similar challenge.
Interviewer: All done? Now, does your code work?
You: I'll run it on a sample array.
Runs through test case where given numbers are in the array, and the method works, even at edges of the array. Method also works when given number is not in the array.
Interviewer: Hmm.. (Quickly checks another test case.) OK looks good. One more thing. The list of numbers can contain duplicates. Can you make sure to return the lowest index in the array that contains the given number?
You: I can just modify my binary search so it searches sequentially to the left once it finds the number to see if there are any more numbers like it.
Interviewer: OK. What would be the running time of your overall approach?
You: The initial sorting should take O(n log n) time. If there are not too many duplicates, finding a number with binary search should take log(n) time.
Interviewer: And what if there are many duplicates?
You: In the worst-case scenario, the entire list will be duplicates of the given number, and then the search would take linear time. But I think the average case would still be O(log n) to find different numbers.
Interviewer: Yes that should be good enough. Now do you have any questions for me?
(Alternatively, interviewer could ask you to improve algorithm so it finds the number in log(n) time even in the worst case.)