Build a normalized search URL with sorted, percent-encoded query parameters
Company: Microsoft
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a system that takes a base URL and query parameters and generates a normalized search URL. The system should handle URL encoding of special characters, support different parameter types (string, integer, boolean, and list), and produce consistent output by sorting parameters lexicographically by key.
Special characters in parameter values must be percent-encoded according to RFC 3986. Spaces are encoded as `%20`. Unreserved characters (letters, digits, `-`, `_`, `.`, `~`) are not encoded. Boolean values are rendered as `true` or `false`. List parameters produce repeated `key=value` pairs, sorted by value.
The system should allow adding, updating, removing, and querying parameters, as well as building the final URL at any point.
Implement the following methods:
- `SearchURLBuilder(base_url: str)` creates a builder for the given base URL with no parameters.
- `addParam(key: str, value: str)` adds or updates a string query parameter with the given key and value.
- `addIntParam(key: str, value: int)` adds or updates an integer query parameter with the given key and value.
- `addBoolParam(key: str, value: bool)` adds or updates a boolean query parameter, rendered as `true` or `false` in the URL.
- `addListParam(key: str, values: List[str])` adds or updates a list parameter. It produces multiple `key=value` pairs in the URL, sorted by value.
- `removeParam(key: str) -> bool` removes the parameter with the given key. It returns `True` if it existed, `False` otherwise.
- `hasParam(key: str) -> bool` returns `True` if a parameter with the given key exists, `False` otherwise.
- `paramCount() -> int` returns the number of distinct parameter keys currently stored.
- `buildURL() -> str` builds and returns the normalized URL with all parameters sorted by key and values URL-encoded.
- `reset()` removes all parameters, keeping the base URL unchanged.
- `getBaseURL() -> str` returns the current base URL.
- `setBaseURL(base_url: str)` updates the base URL to the given value.
Example 1:
```text
builder = SearchURLBuilder("https://example.com/search")
builder.addParam("q", "red shoes & bags")
builder.addIntParam("page", 2)
builder.addBoolParam("in_stock", True)
builder.addListParam("color", ["red", "blue"])
builder.paramCount() # 4
builder.buildURL() # "https://example.com/search?color=blue&color=red&in_stock=true&page=2&q=red%20shoes%20%26%20bags"
```
The keys come out in the order `color`, `in_stock`, `page`, `q`. The list emits one pair per value with `blue` before `red`, and each space in `q` becomes `%20` while `&` becomes `%26`. The list counts as one key, so `paramCount()` is 4.
Example 2:
```text
builder = SearchURLBuilder("https://example.com/find")
builder.addParam("sort", "price")
builder.addParam("sort", "date") # updates the existing key
builder.addParam("tag", "c++")
builder.addParam("name", "José")
builder.addIntParam("limit", 50)
builder.removeParam("limit") # True
builder.removeParam("limit") # False
builder.hasParam("sort") # True
builder.paramCount() # 3
builder.buildURL() # "https://example.com/find?name=Jos%C3%A9&sort=date&tag=c%2B%2B"
builder.reset()
builder.paramCount() # 0
builder.setBaseURL("https://example.org/s")
builder.buildURL() # "https://example.org/s"
```
The second `addParam("sort", ...)` replaces the first value. `+` is outside the unreserved set and becomes `%2B`, and `é` is encoded from its two UTF-8 bytes as `%C3%A9`. With no parameters left, `buildURL()` returns the base URL alone.
```hint What to store per key
Keep each parameter's type next to its value, so `buildURL` knows how to render it and a later update can switch the key to a different type.
```
```hint Encoding pitfall
The unreserved set in RFC 3986 is ASCII only. Check what your language's "is alphanumeric" test returns for a letter like `é`, and percent-encode each byte of a character's UTF-8 form.
```
### Constraints & Assumptions
- The base URL is a non-empty string and is used as given, without encoding.
- Parameter keys contain only alphanumeric characters and underscores.
- String values may contain special characters requiring URL encoding.
- List parameters contain at least one element when added.
- At most 1000 operations will be performed.
- Keys and list values are compared by character code, so uppercase letters sort before lowercase ones. List values are sorted by their raw value, before encoding.
- `buildURL()` returns the base URL with no trailing `?` when no parameters are stored.
### Clarifying Questions to Ask
- Can the base URL already contain a query string or a `#fragment`, and should new parameters be merged into it?
- Are keys case-sensitive, so that `Page` and `page` are two different parameters?
- If a list contains the same value twice, should both pairs be emitted or should duplicates collapse?
### What a Strong Answer Covers
- A storage model that keeps each key's type and value, with add-or-update semantics and constant-time add, remove, lookup and count.
- A percent-encoder that matches RFC 3986: unreserved ASCII kept, space as `%20`, every other character encoded byte by byte from UTF-8 with uppercase hex digits.
- Deterministic output: sorted keys, sorted and repeated list values, and type-specific rendering of integers and booleans.
- Edge cases such as no parameters, a key updated to a different type, an empty string value, and the caller changing a list after adding it.
- The cost of `buildURL` compared with the other operations.
### Follow-up Questions
- How would you handle a base URL that already has a query string or a fragment?
- Many servers decode `+` as a space in query strings. What goes wrong if the builder writes spaces as `+`, and which encoding is safer when the URL is used as a cache key?
- If the normalized URL is a cache key, what other normalizations would you apply to the scheme, host, port and path, and which of them can change what the URL means?
Overview: Design a builder that turns a base URL and typed query parameters into one normalized search URL. It checks RFC 3986 percent-encoding with UTF-8, deterministic sorting of keys and list values, and clear add, update and remove semantics for string, integer, boolean and list parameters.