back to blogs

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:

  1. Understanding the base condition on which we can end the recursion.
  2. 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.
  3. Understanding the expected return type. It’s not necessary that operating on 'object' data type, yields object as result.
  4. 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.
  5. Understanding of basic data types, typeof operator, instanceOf operator, to ensure you can correctly differentiate between different types that still are 'objects'. Examples include arrays, maps, sets, objects, dates, null etc.
  6. 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.
  7. Understanding whether you will operate on nodes or elements on DOM, so you can use the correct API’s for transversing children and siblings.
  8. 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.

  1. Implement a getElementsBy function
  2. Implement a classnames function
  3. Implement a camelCaseKeys function
  4. Implement a compactObject function
  5. Implement a squashObjectKeys function
  6. Implement a removeCycle function
  7. Implement a deepSealObject function
  8. Implement a deepFilterArray function
  9. Implement a deepFlattenArray function
  10. Implement a deepFlattenObject function
  11. Implement a deepEqual function
  12. Implement a deepClone function
  13. Implement a deepMap function
  14. Implement a deepOmit function
  15. Implement a deepMerge function
  16. Implement a generateNthSelector function
  17. Implement a parseHtmlDocument function
  18. 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 instanceOfinstead 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 🫡.