DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
HardJavaScript

Build a Query String Parser and Builder for Environments Without Native URLSearchParams

2 views

Technical Interview Challenge: The "Legacy-Link" Query Parser

Context

Before the modern URL and URLSearchParams APIs were standardized, frontend engineers had to manually manipulate the "Search" portion of a URL. Even today, when building high-performance micro-utilities or supporting highly restricted legacy environments (like older embedded browsers or specific enterprise runtimes), you cannot always rely on native browser APIs.

The challenge is to build a robust Data Structure that can handle the "Multi-Value" nature of query parameters—where a single key can represent multiple values (e.g., ?filter=red&filter=blue)—while maintaining a clean, predictable interface for modification and serialization.


Problem Statement

Implement a class MyURLSearchParams that mimics the standard browser API for parsing and manipulating URL query strings. Your implementation must handle encoding/decoding, multiple values per key, and state synchronization.

Requirements:

  1. Stateful Storage: Store parameters in a way that preserves multiple values for a single key and maintains the order of insertion.

  2. Core Methods:

    • constructor(init): Accepts a query string (optionally starting with ?).

    • get(key) / getAll(key): Retrieves the first value or all values associated with a key.

    • set(key, value): Replaces all existing values for a key with a single new value.

    • append(key, value): Adds a new value to a key without removing existing ones.

    • delete(key): Removes the key and all its associated values entirely.

  3. Utility Methods:

    • has(key): Returns a boolean indicating if a key exists.

    • toString(): Serializes the current state back into a URL-encoded query string.

  4. Edge Case Handling: Handle empty strings, malformed pairs (like key=), and ensure values are properly decoded/encoded.


Example Use Cases

Example 1: Parsing and Retrieval

JavaScript

const params = new MyURLSearchParams("?user=rahul&role=admin&role=editor");

params.get("role");     // Expected: "admin"
params.getAll("role");  // Expected: ["admin", "editor"]

Example 2: Modification and State

JavaScript

params.set("user", "gemini");
params.append("role", "viewer");

params.getAll("role");  // Expected: ["admin", "editor", "viewer"]
params.toString();      // Expected: "user=gemini&role=admin&role=editor&role=viewer"

Example 3: Deletion

JavaScript

params.delete("role");
params.has("role");     // Expected: false
params.toString();      // Expected: "user=gemini"

Interview Evaluation Criteria

  • Data Structure Choice: Will you use a plain Object, a Map, or a Map<string, string[]>? How does your choice impact the toString() order and the append() logic?

  • Encoding/Decoding: Do you correctly use decodeURIComponent during parsing and encodeURIComponent during serialization to handle special characters (like spaces, &, or =)?

  • Order Preservation: When calling toString(), are the parameters returned in the order they were originally parsed or appended?

  • Parsing Logic: How do you handle edge cases like ?key (no equals sign) or && (empty segments)?

Sample Test Cases

Case 1
Input
new("a=1&b=2&a=3").get("a")
Expected Output
"1"
Case 2
Input
getAll("a")
Expected Output
["1","3"]
Case 3
Input
set("a","10").get("a")
Expected Output
"10"

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
2
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering

Languages

JavaScript