Problem: Design a URL Builder with Conditional Parameters

Medium
30 min
Create a fluent URL builder that constructs a complete query string from optional parameters.

Problem statement

You’re building an internal HTTP client wrapper that frequently needs to assemble URLs with optional query parameters—like sort, page, limit, and custom filters.

Currently, developers manually concatenate query parameters, which is error-prone and leads to invalid or inconsistent URLs. You’ve been asked to create a UrlBuilder that helps developers fluently construct URLs with optional query parameters.

Goal

Create a UrlBuilder class with the following chainable methods:

  • .withBase(base: string): Sets the base path for the URL. This is required.

  • .withPage(n: number): Adds a page query parameter.

  • .withLimit(n: number): Adds a limit query parameter.

  • .sortBy(field: string): Adds a sort query parameter.

  • .filterBy(field: string, value: string): Adds an arbitrary query parameter. Can be called multiple times.

  • .build(): Returns the final URL string with all configured parameters encoded.

If no parameters are set, return just the base URL.

Constraints

  • Do not include query parameters that weren’t explicitly set.

  • .withBase() must be called before .build(), or throw an error.

  • Output must be a valid URL string (e.g., /api/items?page=2&sort=name&category=books).

Sample output

The examples below illustrate what the output should look like:

const url1 = new UrlBuilder()
.withBase('/api/items')
.withPage(2)
.withLimit(10)
.sortBy('price')
.filterBy('category', 'books')
.filterBy('tag', 'featured')
.build();
console.log(url1);
/* Expected output:
/api/items?page=2&limit=10&sort=price&category=books&tag=featured */
const url2 = new UrlBuilder()
.withBase('/api/products')
.sortBy('rating')
.build();
console.log(url2);
/* Expected output:
/api/products?sort=rating */
const url3 = new UrlBuilder()
.withBase('/api/events')
.build();
console.log(url3);
/* Expected output:
/api/events */
try {
const url4 = new UrlBuilder()
.withPage(1)
.build();
} catch (err) {
console.log('Failed to build url4:', err.message); // must throw
}
/* Expected output:
Failed to build url4: Base URL is required */

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Problem: Design a URL Builder with Conditional Parameters

Medium
30 min
Create a fluent URL builder that constructs a complete query string from optional parameters.

Problem statement

You’re building an internal HTTP client wrapper that frequently needs to assemble URLs with optional query parameters—like sort, page, limit, and custom filters.

Currently, developers manually concatenate query parameters, which is error-prone and leads to invalid or inconsistent URLs. You’ve been asked to create a UrlBuilder that helps developers fluently construct URLs with optional query parameters.

Goal

Create a UrlBuilder class with the following chainable methods:

  • .withBase(base: string): Sets the base path for the URL. This is required.

  • .withPage(n: number): Adds a page query parameter.

  • .withLimit(n: number): Adds a limit query parameter.

  • .sortBy(field: string): Adds a sort query parameter.

  • .filterBy(field: string, value: string): Adds an arbitrary query parameter. Can be called multiple times.

  • .build(): Returns the final URL string with all configured parameters encoded.

If no parameters are set, return just the base URL.

Constraints

  • Do not include query parameters that weren’t explicitly set.

  • .withBase() must be called before .build(), or throw an error.

  • Output must be a valid URL string (e.g., /api/items?page=2&sort=name&category=books).

Sample output

The examples below illustrate what the output should look like:

const url1 = new UrlBuilder()
.withBase('/api/items')
.withPage(2)
.withLimit(10)
.sortBy('price')
.filterBy('category', 'books')
.filterBy('tag', 'featured')
.build();
console.log(url1);
/* Expected output:
/api/items?page=2&limit=10&sort=price&category=books&tag=featured */
const url2 = new UrlBuilder()
.withBase('/api/products')
.sortBy('rating')
.build();
console.log(url2);
/* Expected output:
/api/products?sort=rating */
const url3 = new UrlBuilder()
.withBase('/api/events')
.build();
console.log(url3);
/* Expected output:
/api/events */
try {
const url4 = new UrlBuilder()
.withPage(1)
.build();
} catch (err) {
console.log('Failed to build url4:', err.message); // must throw
}
/* Expected output:
Failed to build url4: Base URL is required */

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Node.js
class UrlBuilder {
// implement methods here
}
// Example usage:
const url1 = new UrlBuilder()
.withBase('/api/items')
.withPage(2)
.withLimit(10)
.sortBy('price')
.filterBy('category', 'books')
.filterBy('tag', 'featured')
.build();
console.log(url1);
/* Expected output:
/api/items?page=2&limit=10&sort=price&category=books&tag=featured */
const url2 = new UrlBuilder()
.withBase('/api/products')
.sortBy('rating')
.build();
console.log(url2);
/* Expected output:
/api/products?sort=rating */
const url3 = new UrlBuilder()
.withBase('/api/events')
.build();
console.log(url3);
/* Expected output:
/api/events */
try {
const url4 = new UrlBuilder()
.withPage(1)
.build();
} catch (err) {
console.log('Failed to build url4:', err.message); // must throw
}
/* Expected output:
Failed to build url4: Base URL is required */