Flatten Nested Array
Updated
Problem statement
You are given a JSON-style string nested that holds an array of integers with any amount of nesting. Return every integer in left-to-right order as one flat array.
An item of an array may be an integer or another array. Empty arrays add no values. The input may contain whitespace and negative integers.
In the interview, be ready to explain both a recursive way and a non-recursive way to do it. Here, either correct approach is accepted.
Function
flatten(nested: String) → int[]Examples
Example 1
nested = "[1,[2,[3,4],5],6]"return = [1, 2, 3, 4, 5, 6]Walk through the string from left to right and go into every inner array as you meet it. You see the integers in the same order as they are written, so the flat array has them in that order.
Example 2
nested = "[[],[1],[[2,3]],4]"return = [1, 2, 3, 4]The empty arrays add nothing. The deeper arrays are opened as usual, so you collect one, two, three and four in order.
Example 3
nested = "[]"return = []The empty nested array contains no integers.
Example 4
nested = "[-10, [0, -2], 7]"return = [-10, 0, -2, 7]The spaces are ignored, and the negative numbers keep their sign. The flat array is minus ten, zero, minus two, seven.
Constraints
2 <= nested.length <= 100000nestedis a valid JSON-style nested array containing integers and arrays only.- Integers fit in a 32-bit signed value.
- The total number of integers is at most
10000.
More Roblox Phone Screen questions
- Most Frequent Call Path From Function Trace LogsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Search Autocomplete SystemPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Candy Crush Grid Matching and GravityPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Implement a Rate LimiterPHONE SCREEN · Seen Oct 2026
- Rate Limit by Multiple Request FieldsPHONE SCREEN · Seen Oct 2026
- Topological Sort with Secondary OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Time-Aware Search AutocompletePHONE SCREEN · Seen Oct 2026
- Most Frequent Call Stack Per ThreadPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Rate Limiter Sliding Window With Per-Entity LimitsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Closest Binary Search Tree Value — Base PracticePHONE SCREEN · Seen Jun 2026
Similar questions at other companies
- Construct a Tree from Level-Order and Inorder TraversalsAmazon · PHONE SCREEN
- Flatten a Nested ArrayFreshworks · ONSITE INTERVIEWFreshworks interview questions
- Construct a Tree from Level-Order and Inorder TraversalsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Evaluate a Nested Math ExpressionGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Asteroid CollisionSiemens · PHONE SCREEN
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Parse a Partial JSON StreamListen Labs · PHONE SCREEN
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
Roblox interview experiences
- Roblox OA: What It Tests (2026)
- Roblox New Grad Early OA Tasks: 10 Experiences (Part 7 of 7)
- Roblox New Grad Engineer Rounds: 13 Experiences (Part 6 of 7)
- Roblox New Grad HackerRank OA: 18 Experiences (Part 5 of 7)
- Roblox New Grad CodeSignal Finals: 23 Experiences (Part 4 of 7)
- Roblox New Grad Games and BQ: 25 Experiences (Part 3 of 7)