Parse Query Parameters Into a Map
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates ability to implement robust string parsing, type inference, and map/list construction for URL query parameters, including handling duplicate keys, quoted strings, and boolean flag syntax.
Constraints
- 0 <= len(query) <= 100000
- Each non-empty segment is valid and is either `key=value` or `!key`
- Quoted string values use matching double quotes
- For `key=value`, only the first `=` separates the key from the value
Examples
Input: '?key1=1&key1="abc"&key2=value1&!isBooleanField'
Expected Output: {'key1': [1, 'abc'], 'key2': 'value1', 'isBooleanField': True}
Explanation: The key `key1` appears twice, so its parsed values are collected into a list. `key2` appears once as a plain string, and `!isBooleanField` becomes `True`.
Input: '?'
Expected Output: {}
Explanation: After removing the leading `?`, the query is empty, so the result is an empty dictionary.
Input: 'count=-12&name="007"&!active&count=5'
Expected Output: {'count': [-12, 5], 'name': '007', 'active': True}
Explanation: `-12` and `5` are parsed as integers, `"007"` stays a string because it is quoted, and `!active` becomes `True`.
Input: 'user=alice&role=admin&role=editor&city=NY'
Expected Output: {'user': 'alice', 'role': ['admin', 'editor'], 'city': 'NY'}
Explanation: `role` appears twice, so its two string values are stored in order in a list.
Input: '!flag&flag=0&flag="done"'
Expected Output: {'flag': [True, 0, 'done']}
Explanation: The same key first appears as a Boolean flag, then as an integer, then as a quoted string, so all three parsed values are kept in a list.
Input: 'expr=a=b"ed="x=y"'
Expected Output: {'expr': 'a=b', 'quoted': 'x=y'}
Explanation: Only the first `=` splits the key from the value, so `expr` gets the string `a=b`. The quoted value becomes the string `x=y` without the quotes.
Input: '&&a=1&&!b&'
Expected Output: {'a': 1, 'b': True}
Explanation: Empty segments caused by extra `&` characters are ignored.
Hints
- Strip the optional leading `?`, split on `&`, and process each segment one by one.
- Use a dictionary for storage; when a key repeats, convert its existing value into a list and append the new parsed value.