URL Builder
Learn this problemProblem statement
Creating URLs dynamically is a common task when dealing with web applications and APIs. Building these URLs correctly ensures that the desired endpoint is reached with the appropriate parameters.
To build a URL, we define a function buildURL that takes a base URL (baseURL), an endpoint (endpoint), and a list of parameters (params) as a list of key-value pairs. The function concatenates the base URL and endpoint, appends a ? character, and adds the parameters in the format key=value separated by the & character. If the params list contains a page attribute, it should be placed at the end of the URL. Otherwise, the parameters should be built into the URL in the order given in params.
Function
buildURL(baseURL: String, endpoint: String, params: String[][]) → String
Complete the function buildURL in the editor.
buildURL has the following parameters:
- 1.
String baseURL: the base URL - 2.
String endpoint: the endpoint to be appended to the base URL - 3.
String[][] params: a list of two-element arrays representing key-value pairs
Returns
String: the complete URL constructed from the inputs
Examples
Example 1
baseURL = "https://api.example.com"endpoint = "/data"params = [["type", "user"], ["page", "2"], ["id", "123"]]return = "https://api.example.com/data?type=user&id=123&page=2"
For example, given baseURL = "https://api.example.com", endpoint = "/data", and params = [("type", "user"), ("page", "2"), ("id", "123")], the function should return https://api.example.com/data?type=user&id=123&page=2. Note that the page parameter is moved to the end to ensure it is correctly interpreted by the API.
Constraints
baseURLandendpointare non-empty strings that can be concatenated directly.paramscontains at least one pair, and each pair contains exactly two strings: a key and a value.- Parameter keys are unique, so at most one pair has the key
page. - Keys and values are already URL-safe; no additional encoding is required.
- Preserve the input order of non-
pageparameters and move thepagepair, if present, to the end.
More Tiktok problems
- Count Access Code PairsOA · Seen Jul 2026
- Count Key ChangesOA · Seen Jul 2026
- Travel Distance on ScootersOA · Seen Jul 2026
- Count Skipped Numbers After SubtractionsOA · Seen Jul 2026
- Obstacle Placement QueriesOA · Seen Jul 2026
- Repeated Grouped Digit SumOA · Seen Jul 2026
- Count Cyclic Digit PairsOA · Seen Jun 2026
- Event ID Check Completion TimesOA · Seen Jun 2026