12 / 14

Flatten a deeply nested array of numbers and return most frequent number with its count

javascript
Difficulty: 5/10
Topics: recursion, hash maps, time complexity

Scenario Questions

0-2 years experience
  1. 1

    We have a utility function that processes user-submitted survey data, which sometimes comes in as nested arrays of scores like [1, [2, [2, 3]], 1]. How would you write a function to flatten this data and find the most common score and how many times it appeared?

  2. 2

    Imagine you wrote a recursive function to flatten this array, but during testing with a deeply nested array (like 10,000 levels deep), the browser tab crashed with a 'Maximum call stack size exceeded' error. What is happening here, and how would you modify your code to prevent it?

2-5 years experience
  1. 1

    We're building a dashboard widget that aggregates nested category IDs from a legacy API. The API occasionally returns millions of items, and our current flat(Infinity) approach is blocking the main thread, causing the UI to freeze. How would you refactor this to process the data without degrading the user experience?

  2. 2

    A teammate wrote a helper to find the most frequent ID in a nested array using a nested loop over the flattened array. It works fine in staging, but in production with large datasets, it's timing out. How would you debug this performance bottleneck and optimize the time complexity to O(N)?

5-8 years experience
  1. 1

    We are processing massive, deeply nested telemetry payloads in a Node.js microservice. Memory consumption is spiking because we're duplicating arrays during the flattening and frequency-counting phases. How would you design a memory-efficient, streaming or generator-based solution that processes these nested arrays with O(1) auxiliary space?

  2. 2

    You are designing a reusable utility library for a large frontend monorepo. This utility needs to flatten nested structures and compute frequencies. How would you design the API to be highly configurable (e.g., handling custom depth, custom identity selectors for non-primitive elements, and tie-breaking strategies) while maintaining strict TypeScript types?

8+ years experience
  1. 1

    Our analytics platform processes deeply nested event logs across multiple microservices. Some teams use Lodash, some use custom recursive helpers, and others use native ES6 methods, leading to inconsistent performance and memory leaks across the org. How would you define and roll out an organization-wide standard or shared library for high-throughput data normalization, and how would you measure its success?

  2. 2

    We are migrating a legacy real-time data ingestion pipeline from a single-threaded Node.js service to a distributed architecture. The pipeline currently flattens and aggregates nested sensor data in-memory. How would you architect this transition to handle backpressure, ensure high availability, and offload the heavy computation of flattening and aggregation away from the main API gateway?

Follow-up Questions

  • How would you modify your solution to handle ties where multiple numbers have the same maximum frequency?
  • If the input array is extremely deep (e.g., 20,000 levels), how do you prevent a stack overflow in JavaScript?
  • What is the space complexity of your solution, and can we optimize it if we are memory-constrained?