Level-Order Flatten with a Depth Limit
Updated
Problem statement
You get a nested list written as a JSON string, such as "[1,[2,[3]]]". Every item is an integer or another list. The items directly inside the outer list are at level 0, the items inside one of those lists are at level 1, and so on.
Flatten the list level by level, opening lists only down to depth:
- A list at a level less than
depthis opened: its items take its place in the result, one level deeper. - A list at level
depthis not opened. It stays as one whole item, written exactly as in the input. - An opened empty list adds nothing.
Order the kept items by level: every item at level 0 first, then every item at level 1, and so on up to level depth. Items at the same level keep the order in which they appear in nested, from left to right.
Return the result as a JSON string with no spaces. For example, "[1,[2,[3]]]" with depth = 1 gives "[1,2,[3]]".
Function
flattenByLevel(nested: String, depth: int) → StringExamples
Example 1
nested = "[1,[4,[9],6],2,[[0]],3]"depth = 5return = "[1,2,3,4,6,9,0]"Level 0 holds 1, 2 and 3 (the two lists there get opened). Level 1 holds 4 and 6 from [4,[9],6]; the list [0] at level 1 gets opened too. Level 2 holds 9 and then 0. The depth 5 is more than the nesting, so every list is opened.
Example 2
nested = "[[1,[2,[3]]],10,[20,[]]]"depth = 1return = "[10,1,[2,[3]],20,[]]"Level 0 has the number 10, and its two lists are opened because 0 is less than 1. Level 1 is the depth limit, so its lists stay whole: reading left to right it holds 1, [2,[3]], 20 and [].
Example 3
nested = "[3,[1],[]]"depth = 0return = "[3,[1],[]]"With depth 0 no list is opened, so the answer is the input unchanged, empty list included.
Constraints
2 <= nested.length <= 5000nestedis a valid JSON array with no spaces. Its items are integers and arrays, and it uses only the characters0to9,-,,,[and]- Every integer is between
-10^6and10^6and is written without leading zeros (and never as-0) - Every item in
nestedis at level 50 or less 0 <= depth <= 50
More Rippling Phone Screen questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
- Delivery Cost Tracker with Effective-Dated RatesPHONE SCREEN · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best and Worst Camel Card CompletionsPHONE SCREEN · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- In-Memory Priority Task SchedulerPHONE SCREEN · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Median Across N Sorted ArraysPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- N-Ary Tree BFS CodecGoogle · ONSITE INTERVIEWGoogle interview questions
- Evaluate an Arithmetic Expression Written in WordsBrex · PHONE SCREEN
- Top K Movies by Play Count from CSV DataTubi · PHONE SCREEN
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Parse Nested Message TypesApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREENRoblox interview questions
- Shortest Word Chain Between Two WordsFanatics · PHONE SCREEN
Interview reports that mention this question
Rippling interview experiences
- Rippling No-Level Older Reports: 31 Experiences (Part 6 of 6)
- Rippling No-Level Onsite Loops: 24 Experiences (Part 5 of 6)
- Rippling No-Level Screens and Loops: 18 Experiences (Part 4 of 6)
- Rippling No-Level Coding Screens: 37 Experiences (Part 3 of 6)
- Rippling No-Level Design, Screens: 30 Experiences (Part 2 of 6)