Recursion Questions for Frontend Interviews
Notice: This blog series is still in progress. I sometimes publish some content early in case someone finds it useful. Feel free to reach and suggest any new additions or changes.
Welcome to my blog series on JavaScript based recursion questions, tailored for frontend interviews!
In this series, we’ll explore one of programming’s most fundamental yet easily mistaken concepts - recursion. Whether you’re just starting out and want a solid understanding of solving recusion based problems or you’re an experienced developer seeking mastery of recursion techniques, this series caters to all levels of expertise.
I’ll delve into a range of interview questions I’ve encountered firsthand concerning recursion. Each question will be accompanied by detailed solutions and possible variations I have been asked to ensure a comprehensive understanding of the topic.
So, let’s embark on this journey of unraveling recursion together!
Tips on solving recursion based problems
From my personal experience, solving a recursion based problem is not tough at all, as long as one can determine the following:
- Understanding the base condition on which we can end the recursion.
- Understanding the possible values which can be passed, namely edge cases in form of
null
input. Make sure to clarify the question with the interviewer, so as to save time not handling data types which are not expected at all. - Understanding the expected return type. It’s not necessary that operating on
'object'
data type, yields object as result. - Understanding whether or not mutation of the argument is allowed. Make sure to explicity ask the question to the interviewer. It might not seem very obvious and might end up failing 1-2 base test cases.
- Understanding of basic data types,
typeof
operator,instanceOf
operator, to ensure you can correctly differentiate between different types that still are'objects'
. Examples includearrays
,maps
,sets
,objects
,dates
,null
etc. - Understanding whether there will be any cyclic values on input and if so what should be done about it. Most cases, we are expected to remove the cycle and better ask the interviewer early on.
- Understanding whether you will operate on nodes or elements on DOM, so you can use the correct API’s for transversing children and siblings.
- Understanding whether or not inherited keys in objects needs to be considered as well. In almost all cases it should not be considered. You can use
Object.hasOwn()
,object.hasOwnProperty()
or any approach to do so.
Questions asked in interviews
In general, if you have a high level look at the questions, you will notice that recursion based question usually revolves around the idea of nested array or objects data manipulation, implementation for common lodash utilities or in general DOM transversal or DOM filter based problems.
In general, solving these questions along with their variations should be sufficient starting point to excel your recursion based question for your next frontend interview:
Note: You need a working knowledge of DOM API’s, data types in JavaScript and higher order functions for these question.
- Implement a getElementsBy function
- Implement a classnames function
- Implement a camelCaseKeys function
- Implement a compactObject function
- Implement a squashObjectKeys function
- Implement a removeCycle function
- Implement a deepSealObject function
- Implement a deepFilterArray function
- Implement a deepFlattenArray function
- Implement a deepFlattenObject function
- Implement a deepEqual function
- Implement a deepClone function
- Implement a deepMap function
- Implement a deepOmit function
- Implement a deepMerge function
- Implement a generateNthSelector function
- Implement a parseHtmlDocument function
- Implement a serializeHtmlDocument function
Personal tips for interview
As recursion based problem usually deals with nested objects and array or DOM transversal, you need to be familiar with Javscript data types and basic DOM API’s.
Whenever the problem statement would expect you to deal with nested object or arrays, always try to check for edge cases like null
, use utility functions like Array.isArray()
, operators like instanceOf
instead of only relying on typeof
operator which can lead to false positives.
Whenever the problem statement would expect you to deal with DOM transversal, be clear on whether you are operating on nodes or elements and use the appropriate API’s to avoid unnecessary work.
Wishing you best. Happy Interviewing 🫡.