Around 2 years ago, after having written imperative code for about 16 years, I started to learn, explore and use more declarative forms of coding in the creation of Enterprise Software as well as for solving competitive programming algorithms. For me, the result has been clearer, shorter code, with a lot less bugs and issues. Take for example writing your custom code in .NET using loops to order a list based on the age of the people it contains vs using LINQ operators to do it for you. In the first scenario you are doing it all yourself, using for, while or foreach loops. In the second scenario you are just saying what you want done using OrderBy or OrderByDescending, in one line of code, and the .NET framework does the work for you. It’s more succinct, it is clearer and cleaner, and since Microsoft has already tested those operators you know they work. I remember one day during a code review session we were able to convert a method with over 20 lines of codes and two bugs to 4 or 5 lines of code without any bugs in a matter of minutes, using LINQ.

In JavaScript you can do something similar using the Array.prototype methods like map, reduce, and filter. In this way, you can get rid of all the clutter associated with complicated nested loops where in each line of code you tell the runtime how to do things, instead of just saying what you want done.

Being open to and proficient with more declarative approaches also can be the difference between landing your dream job or being rejected. I remember a curious story about Google rejecting Max Howell because he was not able to invert a binary tree during one of the interviews (https://www.quora.com/Whats-the-logic-behind-Google-rejecting-Max-Howell-the-author-of-Homebrew-for-not-being-able-to-invert-a-binary-tree). Max Howell is the creator of Homebrew, which I’ve heard is used by a lot of developers at Google. He has got to be an outstanding developer, that I think Google could have benefited from, and it is a shame that a binary tree stood in his way to land the job.

Inverting a binary tree can be tricky if you try to go with an iterative-imperative solution and you do not remember or know the correct way to do it. But if you go with a declarative approach, using recursion, you can solve the problem in a matter of minutes with very simple 6 or 7 lines of code in languages like JavaScript and C#. Now you might be thinking that recursion is evil, and can devour your stack frames if you are not using a language with support for Tail Call Optimization; that’s a separate discussion, here I was just talking about landing your dream job.

Since inverting a binary tree is simple and does not give much room to see the beauty of declarative thinking and programming, I have selected a more complicated problem to do it. According to leetcode.com, this is one of the most frequently asked problems during interviews at big companies like Google, Facebook, Amazon, Microsoft, Apple, JP Morgan, and so on. I was actually asked to solve it during an interview about two years ago, and even when I was able to do it, I was feeling kind of ashamed of my solution, containing 3 nested for loops, with all those i, j, and k variables all over the place (which happens to be a very common solution you will see on the Internet).

Meet the problem: Letter Combinations of a Phone Number (taken from leetcode.com and also known as T9 by some):