ServiceNow IC2 | HM Round Interview – JavaScript Deep Flatten + Type Grouping (with a Catch)

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2025-06-11Added By Anonymous
Source: Link
•Part 1: Main problem + solution
•Part 2: Follow-up problem (TypeError) + root-cause + fix


💼 HM Round Interview – JavaScript Deep Flatten + Type Grouping (with a Catch)

🔹 Part 1: Main Problem Asked


"You're given a deeply nested array with all kinds of values (numbers, strings, booleans, null, undefined, functions, symbols, objects).


Write a function that:

•✅ Deep flattens the array
•✅ Groups elements by their JavaScript typeof
•✅ Tracks the frequency count of each unique value within that type (This was the followup of the main question)


🔸 Sample Input:

1const input = [1, 'a', [true, ['b', undefined], [{} , null]], () => {}, Symbol('x')];


🔸 Expected Output:

1{ 2 number: { '1': 1 }, 3 string: { 'a': 1, 'b': 1 }, 4 boolean: { 'true': 1 }, 5 undefined: { 'undefined': 1 }, 6 null: { 'null': 1 }, 7 object: { '[object Object]': 1 }, 8 function: { '() => {}': 1 }, 9 symbol: { 'Symbol(x)': 1 } 10}


🔹 Part 2: Follow-Up Challenge – Runtime Error & Fix

While implementing this, I hit a TypeError:

Cannot read properties of undefined (reading '1')


🐞 Root Cause:

1if (!result[key]) { 2 result[key] = {}; 3}


❌ Incorrect check — I was initializing based on key (the value),

but should have checked result[type] instead (the group).


✅ Corrected Code:

1if (!result[type]) { 2 result[type] = {}; 3}


🔍 Bonus Follow-Ups Asked:

•Why does typeof null === 'object'?
•→ Legacy bug. I handled it using: item === null ? 'null' : typeof item.
•How did you stringify symbols safely for keys?
•→ Used item.toString() for Symbol(x) values.
•What if you needed to keep raw values instead of string keys?
•→ Suggested using a Map or { value, count } objects instead of stringified keys.


✅ Outcome:

Handled edge cases, debugging, and recursion with safe typing and clean grouping logic.

Both versions of the solution:


✅ 1. With Frequency Count

1function deepFlattenAndGroupByTypeWithFrequency(input) { 2 const result = {}; 3 4 function recurse(arr) { 5 for (const item of arr) { 6 if (Array.isArray(item)) { 7 recurse(item); // handle nested arrays 8 } else { 9 const type = item === null ? 'null' : typeof item; 10 const key = typeof item === 'symbol' ? item.toString() : String(item); 11 12 if (!result[type]) { 13 result[type] = {}; 14 } 15 16 result[type][key] = (result[type][key] || 0) + 1; 17 } 18 } 19 } 20 21 recurse(input); 22 return result; 23}


🔸 Output Example:

1{ 2 number: { '1': 2 }, 3 string: { 'a': 2, 'b': 1 }, 4 boolean: { 'true': 2 }, 5 undefined: { 'undefined': 1 }, 6 null: { 'null': 1 }, 7 symbol: { 'Symbol(x)': 2 } 8}


✅ 2. Without Frequency Count (Just grouping by type)

1function deepFlattenAndGroupByType(input) { 2 const result = {}; 3 4 function recurse(arr) { 5 for (const item of arr) { 6 if (Array.isArray(item)) { 7 recurse(item); 8 } else { 9 const type = item === null ? 'null' : typeof item; 10 11 if (!result[type]) { 12 result[type] = []; 13 } 14 15 result[type].push(item); 16 } 17 } 18 } 19 20 recurse(input); 21 return result; 22}


🔸 Output Example:

1{ 2 number: [1, 1], 3 string: ['a', 'a', 'b'], 4 boolean: [true, true], 5 undefined: [undefined], 6 null: [null], 7 symbol: [Symbol('x'), Symbol('x')] 8}
Comments