Skip to the solution
MediumFrontendAsked in 20247 min read

Query String Parser

2.9k views

The problem

Problem, as an interviewer would state it: "Write parseQueryString(qs) — the inverse of a query-string generator: convert key=value&key=value2 back into an object, correctly reconstructing a REPEATED key as an array value."

Examples:

Code
parseQueryString("search=hello%20world&tags=js&tags=react&page=2");
// { search: "hello world", tags: ["js", "react"], page: "2" }

Clarifying questions expected:

  • Should a genuinely repeated key produce an array, while a key that appears only once stays a plain string?
  • Are all parsed values genuinely strings, even ones that look numeric (like "2")?
  • Should a leading "?" be stripped automatically, or is the caller responsible for passing a clean string?

Code / implementation expected: Yes — real, direct proof of a full round trip: generate a query string with an array-valued key, parse it back, and confirm the array is correctly reconstructed.

  • string
  • object

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: a full generate-then-parse round trip correctly reconstructs a repeated key as an array and decodes a percent-encoded space

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 62 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Implement a WeakMap-Like API Shape (and Why True Weak References Cannot Be Polyfilled)

Continue