How to Land Your Dream Job Series
Why important to write bug-free code
- Writing solid and bug free code is important especially during interview. It doesn’t have to be bug free at first, but the candidate should be able to quickly find the bug by him/herself and fix it correctly.
- Usually one round of interview would focus on solid code, the coding question would be not that complex, for example LeetCode 2/445: Add Two Numbers. For these kind questions, candidates should pay more attention to code detail, also depend on the exact question, it may be the start question, the interviewer expect you solve in around 15 minutes, and have one more complex question or follow up to ask.
- In this case the candidate has to solve the problem quickly and with solid and bug free code (as it’s hard to know the intention of the interviewer: whether the interviewer cares about detail or not).
- It’s a lot of pressure to write code during interview. It’s better you have a check list or know what to check during the code interview, or at least can quickly find the bug before the interview does.
- We also need practice in our daily work: try to write less bugs at first and check the code to find the bug first before run the test.
Helper methods
- When we think some code we are likely to appear multiple times, and it’s simple but a little bit long to write; or some code it’s a little trick but not related with main logic;
- We can extract it as a separate method: give it a short and meaningful name, we just mention it to the interviewer, and don’y have to implement it now.
- This can make us focus on the main logic and save us time.
The Checklist
Check the change of variables
- This is the most mistakes we made either during interview or everyday.
- Whether we change the pointer(next, current), list(result, temp list), sum correctly.
off-by-one
<(<=) or >(>=)
- To get the min/max, should we use <(<=) or >(>=)?
NPE
the ELSE case
Handle some cases after the loop or merge cases
Usually after the loop, we need handle the case when one list is a longer. If possible we should try to merge these different cased and handle all cases in the loop.
Like in the case LeetCode 2 - Add Two Numbers, we handle the case when list1 or list2 is longer and has more elements in the while loop, so after we don’t have to handle the cases: list1 or list2 has more elements.
Check here for more algorithm interview questions that we can merge cases.
Check precondition
- Validate parameters.
Don’t Forget Comparator
We usually add items to some sorted data structure like: PriorityQueue, TreeSet etc or sort them by some conditions.
It’s very easy to forget to add the comparator: we can use Lambada, or better: just use the not-exist comparator class in the code: as we may use the same comparator class later, and we maybe don’t have to really implement it.
Choose API that is more descriptive, less error-prone
- Use add(remove/Peek)Last/First instead of push or poll (ArrayDeque)
Others
- Overflow: it’s good to mention this, and ask whether you need handle it.
- Good naming for method and variables
Common Mistakes
Binary search
Check here about the different cases in binary search
Best Coding Challenge Websites
- Leetcode
- GeeksforGeeks
- hackerrank
- Big collection of interview preparation links
- TopCoder
- CodeChef
- Codeforces
Books for Algorithm Interview
- Cracking the Coding Interview: 189 Programming Questions and Solutions
- Elements of Programming Interviews in Java: The Insiders’ Guide
- The Algorithm Design Manual
- Grokking Algorithms: An illustrated guide for programmers and other curious people
- Coderust: Hacking the Coding Interview
- Data Structures in Java/Python: : An Interview Refresher