Problem · String
EasyTiktokNEW GRADOA
See Tiktok hiring insights

Problem 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. 1. String baseURL: the base URL
  2. 2. String endpoint: the endpoint to be appended to the base URL
  3. 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

  • baseURL and endpoint are non-empty strings that can be concatenated directly.
  • params contains 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-page parameters and move the page pair, if present, to the end.

More Tiktok problems

drafts saved locally
public String buildURL(String baseURL, String endpoint, String[][] params) {
  // write your code here
}
baseURL"https://api.example.com"
endpoint"/data"
params[["type", "user"], ["page", "2"], ["id", "123"]]
expected"https://api.example.com/data?type=user&id=123&page=2"
checking account