r/programmingchallenges • u/codeobserver • Aug 17 '19
Very few developers can implement this recursive function
It still amazes me that many professional developers have difficulties to create a simple recursive JavaScript function. The following simple question appeared in many hiring interviews and few were able to solve it.
Let’s say we have an array containing numbers and / or other arrays of numbers. Example:
var ar = [2, 4, 10, [12, 4, [100, 99], 4], [3, 2, 99], 0]; We need to create a function to find the maximum number in this array.
This problem has actually 2 solutions. One recursive and one iterative. The recursive one is the most elegant and easy to understand.
Solution 1: Recursive approach. findMax1(ar) is the function that returns the maximum number in this kind of array.
var ar = [2, 4, 10, [12, 4, [100, 99], 4], [3, 2, 99], 0];
var m1 = findMax1(ar);
println("Max 1 = ", m1);
// Use recursion to find the maximum numeric value in an array of arrays
function findMax1(ar)
{
var max = -Infinity;
// Cycle through all the elements of the array
for(var i = 0; i < ar.length; i++)
{
var el = ar[i];
// If an element is of type array then invoke the same function
// to find out the maximum element of that subarray
if ( Array.isArray(el) )
{
el = findMax1( el );
}
if ( el > max )
{
max = el;
}
}
return max;
}Solution 2: Iterative approach. If you are an advanced programmer… please try to solve this without recursion. The problem has also a classic iterative solution. I won’t present the second solution here, but if you are interested to see it please open the “Find Max” tutorial from https://codeguppy.com
5
u/HealyUnit Aug 18 '19
Hey, just because you are too lazy to use modern JavaScript methods doesn't mean the rest of us aren't!
1
u/[deleted] Aug 17 '19
Quick / dirty way to do it:
Not too challenging with a stack:
or a little nicer
1
u/zhongbii Aug 18 '19
var? Which year you are in? 2015? println? What the hell?
1
u/[deleted] Aug 18 '19 edited Aug 18 '19
I could implement it recursively but it would take me more time to come up with the working solution. But this is easier.
Ok, here's recursive version (basically the same as u/ithanlara1) only instead map I used for loop for a change.
I prefer first approach
1
u/CrappyFap69 Aug 18 '19
This is in Go
1
u/Xyliton Aug 25 '19
Also not JS but here it is in Haskell. Please, oh Haskell gods, don't make fun of my atrocious code though. Consider that most of the code size came from me blowing up the array into multiple lines :)