Skip to the code
+25 XP

#4 Menu Breadcrumb Paths

medium

A desktop-style app bar has nested menus. Each menu item is { label } or { label, children }, where children is an array of more items. An item with no children key, or an empty children array, is a command the user can click.

Write menuPaths(items) that returns the full path of every clickable command, written as labels joined by " > ", such as "File > Open > Recent".

List the paths in depth-first order: an item's commands come before the next item at the same level, and siblings keep their order.

Example 1

Input: items = [{"label":"File","children":[{"label":"New"},{"label":"Open","children":[{"label":"Recent"},{"label":"Browse"}]}]},{"label":"Help"}]
Output: ["File > New","File > Open > Recent","File > Open > Browse","Help"]

Open has children, so it isn't a command itself; its two children are.

Example 2

Input: items = []
Output: []

An empty menu has no commands.

Constraints

  • The menu has at most 100,000 items in total.
  • Nesting is at most 20 levels deep.
  • Labels are non-empty and don't contain " > ".
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

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