#8 Interview Preparation | 'Code comprehension' series
Introduction
The ‘Code comprehension’ series is aiming to prepare programmers to corporate coding interviews where they can be requested to read, explain and answer questions about a piece of code. This article is aiming to proceed in a similar way.
For whom is this code challenge ?
- People preparing for coding interviews
- People willing to evaluate their coding skills
- People interested in functional programming (FP)
Additionally information
- The difficulty of this challenge is ranked as : Intermediate
- The topic of this challenge is categorized as : Algorithm
- Programming language : JavaScript
Allowed material to complete this challenge
- Pen
- Paper
Is there any time limit ?
- No time limit for this challenge
Question
Given the below code, what is the output value when evaluating r([10, 20, 11, 12]) ?
Solution
Note : Readers willing to answer this coding question by themselves should stop reading here. Below this point, the solution will be discussed and the answer will be provided.
The function r(…) calls the function rCalc(…) which is a recursive functions and takes two inputs. The functions r and rCalc use some helper functions, here are their meanings.
- head(xs) : returns the first element of the array xs
- tail(xs) : returns a new array containing all the elements of the array xs except its first element
- size(xs) : returns the size of the array xs
Here is the breakdown of the evaluation of r([10, 20, 11, 12]).
As shown above in the execution breakdown, the output of the evaluation of r([10, 20, 11, 12]) returns the array [12, 11, 20, 10]. More generally, the function r(xs) reverses the order of the elements of its input xs.