We’ve talked a lot about different interview problems and algorithms, but being comfortable in an interview setting is a skill in and of itself.

whiteboard

Besides coming up with the right solution, there are a few additional things you want to do in every coding interview.

  1. Ask clarifying questions.
  2. Run through your algorithm & verify with test cases.
  3. Proactively give the time and space complexity.

Many times candidates immediately get wrapped up in writing code and forget to do one or more of these things, and it can count negatively against them. Interviewers are definitely looking for ability to write correct algorithms, but they’re also evaluating candidates based on other skills such as communication and verification.

Ask Clarifying Questions

Before jumping into writing code, a good candidate will always ask clarifying questions. This serves three main purposes:

  • It bounds the problem space. For example, perhaps you ask the interviewer, “Is it guaranteed that all integers in this array are positive?”. If the answer is yes, then you don’t have to account for the entire negative integer space, which could potentially make the problem much easier to solve.
  • It shows the interviewer that you’re proactively thinking about edge cases. Interviews are as much about showing that you’re capable of thinking critically and thoroughly as it is about raw coding ability.
  • It allows you and the interviewer to agree on an understanding of the problem. Occasionally, I’ve given a problem to a candidate and they solved a slightly different problem. Making sure that you and your interviewer are on the same page about what your task is before you begin.

Verify The Algorithm

During an interview, it’s not enough to just write code on the board and call it done. A quality candidate will always step through their algorithm with edge cases and examples to try to identify any bugs. This is a crucial step that almost every interviewer will be looking for.

When verifying, be as thorough as possible. Here are some tips:

  • At each stage of the algorithm, write out what is being stored in any data structures or variables.
  • If doing for- or while-loops, make sure to run the algorithm to completion to catch any off-by-one or termination errors.
  • Pick non-trivial test cases. It may be that the algorithm works for a certain set of inputs, but not for a more complex set. Try to come up with test cases that break the algorithm.

Ideally, the algorithm will be bug-free, but more realistically, you’ll find some small errors. Modifying the algorithm to fix these errors (and running through a few test cases again) will reflect well on your debugging ability.

Time & Space Complexity

Almost every interviewer will ask for the time and space complexity of the algorithm. Proactively providing this information, instead of waiting for them to ask, shows the interviewer that you’re familiar with these concepts and can calculate them easily.

Some things to keep in mind:

  • When initially thinking about the problem, try to think what the absolute best and worst case complexity would be. For example, to find a certain element in an unsorted list, we know that we will have to look at every element in the list at least once, so we’re lower bounded by O(n).
  • Try to “stress test” your complexity, especially if it depends on two or more variables. For example, if your algorithm was O(n * k): What happens if k is extremely small? What happens when k approaches n? Occasionally, you’ll find that one algorithm is optimal for certain values of n and k, and another algorithm is optimal for different values of n and k.
  • For memory constraints, try to reason about if you really need to keep track of all the values in your data structures. Also, be cognizant of the fact that certain functions in different coding languages may use additional memory. A good example of this is list slicing in Python, which creates a new list (potentially another O(n) memory usage).

Coding interviews are difficult, and being fluent in algorithms and coding problems is not always enough to pass. Fortunately, with these three tips, you’ll have a better idea of how to approach a coding interview. Good luck!

Are you interviewing for programming jobs, or do you just enjoy fun programming questions? Check out our newsletter, Daily Coding Problem, to get a question in your inbox every day.