FastPrepLevel-Order Flatten with a Depth Limit
Problems/Level-Order Flatten with a Depth Limit

Level-Order Flatten with a Depth Limit

Rippling logoRippling● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Every rule here is our choice. Items come out grouped by how deeply they sit, shallowest first, and items at the same level keep their left-to-right order from the input. The depth limit counts the outer list's own items as level 0. Lists at a lower level than the limit are opened, lists at the limit stay whole in their place, and an opened empty list adds nothing. The JSON string input and output, the names, the limits and the examples are ours too.

Two reports back this task. The main one, from a first-round frontend interview, shows by one example that numbers in the outer list should come before deeper ones. Its deeper numbers are not in left-to-right order, so the order used here is our own and the example is not reused. The other, from a screening round, asks for flattening down to a given depth and then for a version that moves the nested parts to the back. Neither gives a signature or input limits.

This version checks the order and the depth handling. It does not check JavaScript array code such as a recursive helper or Array.prototype.flat. The editorial covers that.

Our version is about 55% the same as the reported questions.

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 depth is opened: its items take its place in the result, one level deeper.
  • A list at level depth is 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) → String

Examples

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 <= 5000
  • nested is a valid JSON array with no spaces. Its items are integers and arrays, and it uses only the characters 0 to 9, -, ,, [ and ]
  • Every integer is between -10^6 and 10^6 and is written without leading zeros (and never as -0)
  • Every item in nested is 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 Frontend, Mobile, Data: 18 Experiences (Part 12 of 12)

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)
All Rippling interview questionsSee Rippling hiring insights
Saved
class Solution {
    public String flattenByLevel(String nested, int depth) {
        // Write your code here.
    }
}
Checking account…