FastPrepWalk a Nested JSON Object Breadth-First and Depth-First
Problems/Walk a Nested JSON Object Breadth-First and Depth-First

Walk a Nested JSON Object Breadth-First and Depth-First

Adobe logoAdobe● MediumINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem lists the full path of every key in a nested object, in one order chosen by an input: depth-first or level by level. An entry for an engineering internship in San Jose, California (a copy of a review, November 2023, offer accepted) describes one round with two people from the team, one of them the hiring manager. It began with questions about motivation and past work, then a technical part. That part had a single coding question, which named BFS and DFS as possible orders for walking a JSON object. According to the entry, the interviewers wanted a spoken explanation and the candidate's own tests on top of complete code. It gives no input form, output form, limits or examples.

Our choices: the object arrives as compact text with unquoted keys, so every language can read it. Values are objects, integers, true, false or null, and arrays are left out. The two orders are depth-first preorder and level order, picked by a second input. Each visited key is reported by its dot-joined path. Keys of one object keep their written order. The text is at most 3000 characters with at most 30 levels. The examples are new. Writing tests and explaining the code are covered in the editorial and are not tested.

Our version is about 70% the same as the reported question.

You get an object written as text in a compact, JSON-like format:

  • An object is {, then zero or more key:value pairs separated by commas, then }. Keys are not quoted.
  • A value is another object, an integer, or one of the words true, false and null.

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 <= 3000
  • text is 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 0 is fine, -0 is not) and is between -10^9 and 10^9.
  • Objects are nested at most 30 levels deep, counting the outer object.
  • order is "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

Interview reports that mention this question

  • Adobe SWE Intern Interviews: 27 Experiences (Part 2 of 4)
  • Adobe Intern Interview Questions: 54 Experiences (Part 1 of 4)

Adobe interview experiences

  • Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
  • Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
  • Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
  • Adobe New Grad MTS Interview Questions: 7 Experiences
All Adobe interview questionsSee Adobe hiring insights
Saved
class Solution {
    public String[] traverseKeys(String text, String order) {
        // Write your code here.
    }
}
Checking account…