So, as I go through the process of interviewing for new roles I find myself starting to challenge myself with interview questions I find online.

This particular one is taken from LeetCode which is an excellent site that contains great content to both learn and test that knowledge with their interview questions as well as mocks.

The question

So, the question is called Add Two Numbers, but you know as it's an interview type question it's not that simple.

By the way, if you want to check this question out on the LeetCode site and try doing it for yourself - you can do so by clicking this link Add Two Numbers.

So, to paraphrase the question. Basically, you get two values passed in using linked list in reverse order.

So, the number 362 for example would get passed in as:

[2] => [6] => [3]

So, 2 numbers get passed in this format. You need to add the two numbers together and then return the result as a linked list in the same format.

So the way I approached this was to add each number in turn, and I will come to that - but first...

My first approach was to multiple each value. So the first value in the head of the list was multiplied by 1. Then I added the second value multiplied by 10 and the third multiplied by 100. To make sure I had no limit, the multiplier was multiplied by 10 each time - so the 4th would have been 1000 and so on.

I did this for both values, then on the reverse I divided using modular to get the remainder for each individual value.

This was long winded and although I could get it to work - it was prone to issues and errors.

So, brushing this aside I started on my second and final answer. Now, from the first iteration, I knew dividing by 10would give me some answers I needed. Such as the carry-over.

The carry-over would be something like 9x9=18 - 10 is carried.

Another thing I though is, that, instead of working out the number and then doing calculations - I could do for each individual value in the list.

So, the values for the heads of both list1 and list2 could be added together. Then we can divide by 10 to get the carry-over value and a modulus to work out to get the value to add to the 3rd list.

This value gets added to a new list node and it iterates through the list.

But, instead of me continuing to talk - why don't I just give you the working code to go through. This following code is the code I wrote and tested in LeetCode.

``public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {         ListNode root = new ListNode(0);;         ListNode l3 = root;                  int carry = 0;         while (l1 != null || l2 != null)         {                      int l1_val = (l1 != null) ? l1.val : 0;             int l2_val = (l2 != null) ? l2.val : 0;                          int totalVal = l1_val + l2_val + carry;                          carry = totalVal/10;             int finalVal = totalVal%10;                          ListNode new_node = new ListNode(finalVal);                                       if (l1 != null) l1 = l1.next;             if (l2 != null) l2 = l2.next;                          l3.next = new_node;                          l3 = l3.next;         }                  if (carry > 0)         {             ListNode carryNode = new ListNode(carry);             l3.next = carryNode;             l3 = l3.next;         }                  return root.next;     }``

Hopefully, going through this will give you an idea of how to answer any similar question you may get in an interview. Also, definitely check out LeetCode (I'm not affiliated by the way) as it is a fantastic place to get many more questions like this to test yourself on.

This site uses Akismet to reduce spam. Learn how your comment data is processed.