Flatten JSON With Array Indices
Updated
Problem statement
You get nested data written as text in a compact, JSON-like style without quotes:
- An object is
{, then zero or morekey:valuepairs separated by commas, then}. - An array is
[, then zero or more values separated by commas, then]. - A value is an object, an array, or a plain value. Keys and plain values are runs of letters and digits.
The whole text is one object. Flatten it: for every plain value, output the pair [path, value]. The path joins, with _, every key and array position on the way from the outer object down to the value; array positions count from 0. For example, in {k:[x,{y:z}]} the value z has the path k_1_y.
Return the pairs in the order their values appear in text. Empty objects and empty arrays hold no values, so they add no pairs, but they still take up a position in an array.
Function
flattenJson(text: String) → List<List<String>>Examples
Example 1
text = "{user:{name:ana,tags:[red,blue]},id:7}"return = [["user_name","ana"],["user_tags_0","red"],["user_tags_1","blue"],["id","7"]]name sits inside user, so its path is user_name. The array tags holds red at index 0 and blue at index 1, giving user_tags_0 and user_tags_1. Then id holds 7. The pairs follow the order of the values in the text.
Example 2
text = "{grid:[[5,8],[3]]}"return = [["grid_0_0","5"],["grid_0_1","8"],["grid_1_0","3"]]grid is an array of arrays. 5 and 8 are items 0 and 1 of item 0, and 3 is item 0 of item 1, so the paths are grid_0_0, grid_0_1 and grid_1_0.
Example 3
text = "{empty:{},list:[],v:0}"return = [["v","0"]]An empty object and an empty array hold no values, so they add no pairs. Only v remains.
Constraints
2 <= text.length <= 2000textis one object in the format above, with no spaces.- Keys and plain values are runs of English letters and digits, and the keys inside one object are all different.
- Brackets are nested at most
100deep, counting the outer object.
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
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Decode StringSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
- Plus and Minus CalculatorIndeed · PHONE SCREENIndeed interview questions
- Arithmetic Expression EvaluatorMicrosoft · PHONE SCREENMicrosoft interview questions
- Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
- Evaluate an Arithmetic Expression with DivisionMotive · ONSITE INTERVIEWMotive interview questions
- Parse a Partial JSON StreamListen Labs · PHONE SCREEN
- Parse Nested Message TypesApplied Intuition · PHONE SCREENApplied Intuition interview questions
Interview reports that mention this question
Adobe interview experiences
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe New Grad MTS Interview Questions: 7 Experiences
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)
- Adobe Product Intern Interviews: 15 Experiences (Part 3 of 4)