FastPrepFlatten JSON into Repeated Key-Value Pairs

Flatten JSON into Repeated Key-Value Pairs

Klaviyo logoKlaviyo● MediumFULLTIMENEW GRADONSITE INTERVIEW
Learn

Problem statement

Flatten a nested JSON value into an ordered list of key-value pairs. Join nested object keys with dots. Arrays add no path segment: their scalar leaves repeat the current path, even when several pairs have identical keys or values.

Input representation

For this exercise, the practice representation is an ordered String[][] nodes table, so each language receives the same JSON tree structure. Row 0 is the root. This table represents an already parsed JSON value; JSON text parsing is not part of the task.

  • ["OBJECT", key1, child1, key2, child2, ...] is an object. Each key is followed by its child-node index. The pair order is the object's member order.
  • ["ARRAY", child1, child2, ...] is an array with children in element order.
  • ["VALUE", scalarJson] is one scalar. scalarJson is a valid JSON encoding of a string, number, boolean, or null. Copy this text unchanged into the output; do not decode or normalize it.

Flattening rules

  1. Begin with an empty path.
  2. Visit object members in their encoded order. Append the member key to the path, separated by a dot when the path is already nonempty, and traverse its value.
  3. Visit array elements from left to right using the same path. Do not add array indexes.
  4. At a VALUE row, append [path, scalarJson] to the answer.
  5. Empty objects and arrays contribute no output. Restore the parent path before visiting the next sibling.

Return String[][] in depth-first encounter order. Keep repeated paths and repeated identical pairs. Every scalar leaf is guaranteed to have at least one object-key ancestor, so every emitted path is nonempty.

Interview follow-up

A related report asks about reversing the input and output. After solving the flattening task, explain whether the pair list uniquely determines the original JSON and what additional structure an invertible format would need. The judged function returns only the flattened pairs; it does not reconstruct JSON.

Function

flattenJsonPairs(nodes: String[][]) → String[][]

Examples

Example 1

nodes = [["OBJECT","a","1"],["OBJECT","b","2","c","3"],["VALUE","\"foo\""],["ARRAY","4","5","6"],["VALUE","4"],["VALUE","5"],["VALUE","6"]]return = [["a.b","\"foo\""],["a.c","4"],["a.c","5"],["a.c","6"]]

The reported native example is flatten({'a': {'b': 'foo', 'c': [4,5,6]}}) = [('a.b', 'foo'), ('a.c', 4), ('a.c', 5), ('a.c', 6)]. The node table encodes that same input. The second output column uses JSON text, so the string value foo includes its JSON quotes. Array elements repeat a.c; no index is added.

Example 2

nodes = [["ARRAY","1","5"],["OBJECT","z","2","a","3"],["VALUE","2"],["OBJECT","x","4"],["VALUE","3"],["OBJECT","z","6"],["VALUE","4"]]return = [["z","2"],["a.x","3"],["z","4"]]

The root array contributes no path segment. Visit the first object in its encoded order, then the second object. The two z rows remain separate.

Example 3

nodes = [["OBJECT","items","1","empty","6"],["ARRAY","2","3","4","5"],["ARRAY"],["OBJECT"],["VALUE","\"x\""],["VALUE","null"],["OBJECT"]]return = [["items","\"x\""],["items","null"]]

Empty arrays and objects emit no pair. The string and null leaves retain the same items path and their different JSON scalar encodings.

Constraints

  • 1 <= nodes.length <= 10000. The tree has at most 100 edges on a root-to-leaf path.
  • Every row is well formed. Child indexes are decimal integers that name later rows; every nonroot row has exactly one parent and every row is reachable from row 0.
  • The root is OBJECT or ARRAY. An ARRAY root contains at least one OBJECT descendant. Every scalar leaf has at least one object-key ancestor.
  • Keys contain 1 to 30 ASCII letters, digits, or underscores, so keys never contain dots. Keys are unique within each object.
  • Each scalar token has at most 1000 characters. The combined input-field length and the combined output-string length are each at most 200000.

More Klaviyo problems

See Klaviyo hiring insights
public String[][] flattenJsonPairs(String[][] nodes) {
    // Write your code here
}
nodes[["OBJECT","a","1"],["OBJECT","b","2","c","3"],["VALUE","\"foo\""],["ARRAY","4","5","6"],["VALUE","4"],["VALUE","5"],["VALUE","6"]]
expected[["a.b", "\"foo\""], ["a.c", "4"], ["a.c", "5"], ["a.c", "6"]]
Checking account…