PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Software Engineering Fundamentals/Elastic

Review a JavaScript flatten PR

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's understanding of JavaScript recursion, array manipulation, API contract reasoning, and correctness when implementing a flatten utility.

  • medium
  • Elastic
  • Software Engineering Fundamentals
  • Software Engineer

Review a JavaScript flatten PR

Company: Elastic

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

## Live PR Review: JavaScript `flatten` using recursion You are reviewing a pull request that introduces a `flatten` utility. ### Intended behavior Implement `flatten(input)` such that: - `input` is an Array that may contain nested Arrays to arbitrary depth. - The function returns a **new** flat Array preserving left-to-right order. Examples: - `flatten([1, [2, 3], [[4]], 5]) -> [1, 2, 3, 4, 5]` - `flatten([]) -> []` - `flatten([1, [2, [3]]]) -> [1, 2, 3]` ### PR code under review ```js // PR version export function flatten(arr) { const out = []; function helper(x) { for (const i in x) { const v = x[i]; if (typeof v === 'object') { helper(v); } else { out.push(v); } } } helper(arr); return out; } ``` ### Tasks 1. Identify **correctness bugs** and **edge cases** this implementation fails. 2. Discuss **API/contract** questions you would clarify (e.g., how to treat non-array objects, `null`, sparse arrays, strings). 3. Suggest improvements for: - readability and maintainability - performance (time/space) - safety (e.g., stack overflow risk) 4. Propose a revised approach (pseudocode or description is fine) and a minimal set of tests you would request before approving.

Quick Answer: This question evaluates a candidate's understanding of JavaScript recursion, array manipulation, API contract reasoning, and correctness when implementing a flatten utility.

Elastic logo
Elastic
Nov 13, 2025, 12:00 AM
Software Engineer
Technical Screen
Software Engineering Fundamentals
1
0

Live PR Review: JavaScript flatten using recursion

You are reviewing a pull request that introduces a flatten utility.

Intended behavior

Implement flatten(input) such that:

  • input is an Array that may contain nested Arrays to arbitrary depth.
  • The function returns a new flat Array preserving left-to-right order.

Examples:

  • flatten([1, [2, 3], [[4]], 5]) -> [1, 2, 3, 4, 5]
  • flatten([]) -> []
  • flatten([1, [2, [3]]]) -> [1, 2, 3]

PR code under review

// PR version
export function flatten(arr) {
  const out = [];

  function helper(x) {
    for (const i in x) {
      const v = x[i];
      if (typeof v === 'object') {
        helper(v);
      } else {
        out.push(v);
      }
    }
  }

  helper(arr);
  return out;
}

Tasks

  1. Identify correctness bugs and edge cases this implementation fails.
  2. Discuss API/contract questions you would clarify (e.g., how to treat non-array objects, null , sparse arrays, strings).
  3. Suggest improvements for:
    • readability and maintainability
    • performance (time/space)
    • safety (e.g., stack overflow risk)
  4. Propose a revised approach (pseudocode or description is fine) and a minimal set of tests you would request before approving.

Solution

Show

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Software Engineering Fundamentals•More Elastic•More Software Engineer•Elastic Software Engineer•Elastic Software Engineering Fundamentals•Software Engineer Software Engineering Fundamentals
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.