Problem, as an interviewer would state it:
"Write toQueryString(obj) — convert a plain object into a URL query string, correctly percent-encoding values, handling array values as repeated keys, and skipping null/undefined."
Examples:
toQueryString({ search: "hello world", tags: ["js", "react"], page: 2 });
// "search=hello%20world&tags=js&tags=react&page=2"
Clarifying questions expected:
- Should array values become repeated
key=valpairs, or a single comma-joined value? - Should
null/undefinedvalues be skipped entirely, or included as an empty string? - Does the key itself ever need encoding too, not just the value?
Code / implementation expected: Yes — real, direct proof handling a string with a space, an array value, a number, and an undefined field, verified against real encodeURIComponent output.