SeanMcCammon C# .Net core 3 Software Developer

When I first started as a Software Engineer in the 90's (1990's to be clear - just in case this is still up in 2100), there were still many languages being developed in, but the major players were C/C++ with some Perl, Ada, Cobol and a few others.

Most systems were being written in C/C++ and running generally on Unix systems. So, that is what I learnt. I started working for companies creating software for Unix platforms written in C and later C++.

It was fun, interesting and you didn't need to be too adaptable. You may need to know some Unix script. How to create a makefile. Do a little Awk or Perl, In regards to language, life was quite simple.

Now, I'm not saying that there were not jobs in other languages. They were still out there - especially as the year 2000 approached and there was a lot of roles for Cobol developers. The reality then was, there were far less languages or subsets of languages out there that we could do work in.

Learning some Java

Moving into the 2000's I found myself working with a little Java. I worked on a project that had a Java front end, C server and Perl backend. The idea was, request were made in the Java UI, passed via a pipeline to the C server, then this would make some requests to Perl backend.

I'd done some Java back in the mid 90's while at Uni. So, I had some understanding. However, it had moved on during that time and the interface code had too.

This was my first real experience of being adaptable.

More languages to come

Later in the 2000's my worked moved into some C++ on Windows which also evolved into doing some work with C#. I was spending more time looking at code in different languages.

Learning to become adaptable, looking at the structure of the code rather than the syntax. Now, there are definitely some nuance in languages - they do things different. However, the way a program is structured remains the same (well, that is until we started to move into more Microservices).

As new languages evolved, subsets of languages and tools that did very specific parts of an application - we had to adapt. We started to become more full stack developers. Someone that would create an application using different levels of language. You may use Javascript for the UI. You may use something like C#, Java, Python or similar for more of the back office with SQL or a no-SQL data storage. We become more adaptable.

Today we need to be adaptable

Without giving too much history - we move to today. In the last 12 months I've worked on projects in C#, Javascript, Python and Java (with Sprint Boot). Some of these I did not have a great grasp on to begin with, but we adapt.

I now look at code as it is structured. Python aside, most languages structure very similar. So, you can follow along the code - with the minor most lookup on specific syntax your not sure of.

You can also create code in a similar manner. You create the idea, the structure (I still love UML). You then create the skeleton. Finally, you start to fill the methods to do what each is needed.

Creating a well structured skeleton will allow you to keep methods to small segments of code doing very specific things.

We also need to adapt to new design models. Today we see more Microservice models used - which in my humble opinion is really the next evolution from having objects in your code.

Small, self-contained services that do just specific things. They maintain their own data (usually in a DB) and don't really care about what is using them. They simply give an interface (usually API) link and just manage the input and output.

It's quite probable that in 5 or 10 years time a new model will be in use. New languages. New tools and techniques. What won't be new is that we will need to be adaptable - being able to switch from 1 platform to the next, 1 language to another.

Being a Software Engineer in today's technology world is becoming more interesting every day. The more you can move from one stack to the next will be your strength. Flex it and train it...

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.

My answer

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

First answer iteration

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.

Second answer iteration

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.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram