10 / 14

Find maximum and minimum number of an array.

Example code:
Difficulty: 4/10
Topics: Array reduction, Memory limits, Performance optimization

Scenario Questions

0-2 years experience
  1. 1

    We are building a simple dashboard widget that displays the lowest and highest temperature recorded today from an array of hourly readings. How would you write a function to extract these two values? What should your code return if the sensor data array is empty?

  2. 2

    A colleague used Math.min(...temperatures) and Math.max(...temperatures) to find the range of a dataset. It works fine in local testing, but what might go wrong with this approach if the dataset grows to tens of thousands of items, and how would you write a safer alternative?

2-5 years experience
  1. 1

    We have a production bug where a telemetry service crashes with a 'Maximum call stack size exceeded' error when processing large batches of user event timestamps. The code currently uses Math.max(...timestamps). Why is this happening, and how would you refactor this to be memory-safe while keeping it performant?

  2. 2

    Imagine you are writing a utility function to find the min and max values of a dataset, but the raw data from the API sometimes contains dirty values like null, undefined, or string numbers like '42'. How would you design a single-pass reducer that filters out these anomalies and returns the correct min/max without iterating over the array multiple times?

5-8 years experience
  1. 1

    We are building a real-time charting library in React that needs to calculate the y-axis bounds (min/max) of a streaming dataset of financial ticks (up to 500,000 points) at 60fps. A naive recalculation on every tick causes frame drops. How would you architect the state and calculation logic to keep this rendering performant?

  2. 2

    You are reviewing a PR where a developer implemented a Web Worker to calculate the min/max of a massive 50MB Float32Array to avoid blocking the main thread. However, transferring the array to the worker is causing a noticeable UI stutter. How would you optimize the data transfer and the calculation strategy here?

8+ years experience
  1. 1

    Our platform processes high-throughput IoT sensor streams across multiple microservices. We need to expose real-time min/max metrics over arbitrary sliding time windows. How would you design the data ingestion and aggregation pipeline to compute these metrics efficiently without storing the raw arrays in memory?

  2. 2

    We are designing a core analytics SDK that will be embedded in hundreds of third-party websites. We need to calculate performance metrics (min/max/percentiles) of user interactions. Since we don't control the host environment's memory limits or JS engine constraints, how would you design this SDK's data collection and aggregation layer to guarantee a zero-allocation or near-zero-allocation footprint?

Follow-up Questions

  • What is the maximum array size your solution can handle before hitting engine-specific memory or call stack limits?
  • How would you modify your approach if the array was a stream of numbers arriving over a WebSocket rather than a static in-memory array?
  • If the array contains NaN or undefined values, how does your current implementation behave, and how would you make it resilient?