FastPrepFlatten a Mixed JSON List

Flatten a Mixed JSON List

Nike, Inc. logoNike, Inc.● MediumFULLTIMEONSITE INTERVIEW

Problem statement

Flatten a nested list that may contain JSON values and object records. The string encoded is a compact JSON encoding of the list.

  • JSON arrays represent lists. Recursively replace each list with its elements, keeping left-to-right order.
  • Every non-array JSON value is an atomic leaf: a number, string, boolean, null, or object.
  • Objects stay whole, even when their fields contain arrays or other objects. Other object types are represented by opaque JSON object records; preserve those records without inspecting their fields.

Return a String[] containing the exact JSON text of each leaf. Keep quotes and escapes around string leaves and preserve the original text inside object leaves. Empty lists contribute no leaves. Do not parse or reformat an atomic leaf.

Function

flattenMixedList(encoded: String) → String[]

Examples

Example 1

encoded = "[1,[\"x\",{\"a\":[2,3]}],null,[],[true]]"return = ["1","\"x\"","{\"a\":[2,3]}","null","true"]

The list containing "x" and an object is expanded. The object {"a":[2,3]} remains one leaf, its inner array stays inside the object, and the empty list adds nothing.

Example 2

encoded = "[[[],[2]],{\"kind\":\"Widget\",\"id\":7}]"return = ["2","{\"kind\":\"Widget\",\"id\":7}"]

Only the number 2 survives the nested lists. The Widget object record is kept as one unchanged leaf.

Constraints

  • 2 <= encoded.length <= 50000.
  • encoded is valid JSON with an array at its root and no whitespace outside quoted strings. JSON container nesting is at most 200.

More Nike, Inc. problems

See Nike, Inc. hiring insights
Saved
public String[] flattenMixedList(String encoded) {
    // write your code here
}
Checking account…