I was diagnosed with ADHD in 2021 after working at Amazon for more than three years as a software engineer at Alexa Smart Properties.
While the diagnosis did not shock my spouse Michal and some of my friends to me, it started a chain of thoughts, insights, and self-reflections, including my past performance as an interviewer.
When I founded Lida Group Consulting on April 2022, I decided to provide our customers with tips and tools to reduce coding mistakes caused by a reduced concentration level.
Many coding interviews are based on data structures and algorithms, two of the most fundamental blocks of computer science.
Candidates are presented with a problem and are asked to provide a working program with optimal time and space complexity.
Linked lists are among the most popular data structures in these interviews. In this article, I will teach you some simple “programming aids” that I use to reduce null pointer bugs relating to linked lists.
I will use the classic “Merge two sorted linked lists” problem to demonstrate some safeguards that can be applied for dealing with Linked-Lists.
First, let’s observe a Java solution with no safeguards:
Possible pitfalls:
Although the above code is correct, candidates may encounter a few common pitfalls as listed below:
- Trying to access a value of a null node
- Trying to advance to a next node of a null node
- Forgetting to handle the tail of the longer list
Safe-call and Elvis operators in Kotlin programming language as the source of the idea for safeguards
Kotlin is a JVM-based language developed by JetBrains, the company behind IntelliJ IDE, one of the most popular IDEs for Java development.
Kotlin is a strongly typed language that includes non-nullable and nullable types, a feature that the language inventors found lacking in Java.
A nullable type is a type whose values may be null.
The Elvis operator, which got its name from its syntax of “?.” and “?:” , provides a safe way to access nullable values.
“a?.b” is a safe-call operator, which means that if a is null, b will not be accessed or invoked (if b is a function).
“a ?: b” is the Elvis operator, which means that if a is null, the value of b will be used to evaluate an expression.
Introduction of safeguards to the above code
We can perform a safe traversal of a list by introducing a “getNext” method that will return the next of a given node if not null or null in case it is null.
We can perform a safe value retrieval by introducing a “getValue” method that will return the value of a given node if not null or a default value if null.
The code can then look like this:
As it is guaranteed that getValue returns the maximum value if the node is null, we can remove the checks after the while loop – when a list terminates; we can safely continue to iterate over it, returning maximum values that will never be inserted into the merged list.
The while condition is changed to allow cases where l1 or l2 are empty.
Though the execution of the above code will invoke more “if” checks, it still conforms to a linear time complexity.
