Skip to the code
cf.
+10 XP

#5 Deepest Layer Group

easy

In a design tool, a layer list is an array whose items are layer ids (numbers) or groups. A group is another array with the same rules, so groups can hold groups.

Write nestingDepth(layers) that returns how deep the nesting goes. A list with no groups inside it has depth 1. A list that contains groups has depth 1 plus the largest depth among its groups. An empty list has depth 1.

Example 1

Input: layers = [1,[2,[3]],4]
Output: 3

The outer list holds [2, [3]], which holds [3]: three levels.

Example 2

Input: layers = [1,2,3]
Output: 1

No groups, so the depth is 1.

Constraints

  • The structure holds at most 1000 items in total.
  • Nesting is at most 100 levels deep.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).