How to Write Bug Free Code During Interview


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

Check here about the different cases in binary search

Best Coding Challenge Websites

Books for Algorithm Interview

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)