FastPrepFastPrep
Problem Brief

Parse Query String

FULLTIMEPHONE SCREEN

Given a GET request query string, parse the key-value pairs into a normalized representation.

The input may start with ?. Parameters are separated by &. A parameter can have one of these forms:

  • key=value: stores the parsed value for key.
  • !key: stores the boolean value true for key.

Keys may repeat. If a key appears more than once, preserve all of its values in insertion order.

Values can be integers, booleans, quoted strings, or list literals. For this function, return a stable string serialization of the parsed map:

  • Return one entry per distinct key.
  • Keep keys in the order they first appear.
  • Represent a single value as key=value.
  • Represent multiple values as key=value1|value2|....
  • Remove surrounding double quotes from quoted string values.

1Example 1

Input
query = "?key1=1&key1=\"abc\"&key2=value1&!isBooleanField"
Output
["key1=1|abc","key2=value1","isBooleanField=true"]
Explanation

key1 appears twice, so both values are preserved. The boolean shorthand !isBooleanField stores true.

2Example 2

Input
query = "page=2&active=false&tags=[red,blue]&page=3"
Output
["page=2|3","active=false","tags=red|blue"]
Explanation

The parser keeps the first-seen key order: page, active, then tags.

Constraints

Limits and guarantees your solution can rely on.

  • 0 <= query.length <= 10^5
  • Keys are non-empty and contain letters, digits, underscores, or hyphens.
  • Values do not contain unescaped &.
public String[] parseQueryString(String query) {
    // write your code here
}
Input

query

"?key1=1&key1=\"abc\"&key2=value1&!isBooleanField"

Output

["key1=1|abc", "key2=value1", "isBooleanField=true"]

Sign in to submit your solution.