Walk a Nested JSON Object Breadth-First and Depth-First
Updated
Problem statement
You get an object written as text in a compact, JSON-like format:
- An object is
{, then zero or morekey:valuepairs separated by commas, then}. Keys are not quoted. - A value is another object, an integer, or one of the words
true,falseandnull.
Think of the object as a tree: every key is a node, and the keys inside a nested object are the children of the key that holds it. Name each key by its path: the keys from the outer object down to it, joined with dots, for example cart.items.pen.
Visit every key, including keys whose value is an object, and return their paths in the visiting order given by order:
"dfs"(depth-first): visit a key, then everything inside its value, before moving to the next key of the same object."bfs"(breadth-first): visit all keys of the outer object, then all keys one level down, and so on. Within a level, keys come in the order of their parent keys on the level above, and keys under the same parent keep their written order.
In both orders the keys of one object are taken in the order they are written. An empty object {} has no keys.
Function
traverseKeys(text: String, order: String) → String[]Examples
Example 1
text = "{cart:{items:{pen:2,ink:{blue:1}},total:9},user:{id:4},ok:true}"order = "bfs"return = ["cart","user","ok","cart.items","cart.total","user.id","cart.items.pen","cart.items.ink","cart.items.ink.blue"]Level 1 holds cart, user and ok. Level 2 holds the keys inside them, in the same order: cart.items, cart.total, then user.id. Level 3 is cart.items.pen and cart.items.ink, and level 4 is cart.items.ink.blue.
Example 2
text = "{cart:{items:{pen:2,ink:{blue:1}},total:9},user:{id:4},ok:true}"order = "dfs"return = ["cart","cart.items","cart.items.pen","cart.items.ink","cart.items.ink.blue","cart.total","user","user.id","ok"]Depth-first goes as deep as it can before moving on: cart, then everything inside cart (items, its keys pen and ink, then ink.blue, then total), and only then user, user.id and ok.
Example 3
text = "{a:{},b:null}"order = "bfs"return = ["a","b"]The empty object under a has no keys, so only a and b are visited.
Constraints
2 <= text.length <= 3000textis one valid object in the format above, with no spaces.- Each key is 1 to 10 lowercase letters or digits, and keys inside one object are all different.
- Each integer is written without leading zeros (a lone
0is fine,-0is not) and is between-10^9and10^9. - Objects are nested at most 30 levels deep, counting the outer object.
orderis"bfs"or"dfs".
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Evaluate DivisionApple · PHONE SCREENApple interview questions
- Tree Height After Child PromotionSnowflake · PHONE SCREENSnowflake interview questions
- Possible BipartitionByteDance · PHONE SCREENByteDance interview questions
- Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Route Graph Reachability with a Broken LinkFigma · PHONE SCREENFigma interview questions