fbpx

#kotlinDevChallenge – 19

Create a Kotlin function that takes a URL string as input and extracts various components from it, such as the protocol, domain, path, and query parameters.

Task Description:

  1. Implement a function named extractUrlComponents that accepts a URL string.
  2. The function should return a data class UrlComponents containing the extracted components: protocol, domain, path, and a map of query parameters.
  3. Handle different URL formats and edge cases, such as URLs without certain components (e.g., missing query parameters).

Test cases

/**
 * Test Cases:
 * - extractUrlComponents("http://www.example.com/test/path?query=123&item=value")
 *   should return UrlComponents(protocol="http", domain="www.example.com", path="/test/path", queryParams={"query"="123", "item"="value"})
 * 
 * - extractUrlComponents("https://sub.example.com/path?param=value")
 *   should return UrlComponents(protocol="https", domain="sub.example.com", path="/path", queryParams={"param"="value"})
 *
 * - extractUrlComponents("https://www.example.com/path?name=&age=30")
 *   should return UrlComponents(protocol="https", domain="www.example.com", path="/path", queryParams={"name"="", "age"="30"})
 *
 * - extractUrlComponents("http://www.example.com/path/to/resource?param1=value1&param2=value2")
 *   should return UrlComponents(protocol="http", domain="www.example.com", path="/path/to/resource", queryParams={"param1"="value1", "param2"="value2"})
 */
  1. Fill in the code.
  2. You can copy the code into IntelliJ/Android Studio, but it shouldn’t be necessary.
  3. Click “run” – the green triangle. The code will compile and several tests will be run.
  4. In the logs, you will see “try again” if the solution is incorrect, or “success” and a special code.
  5. Provide this code in the form below along with your email and click “send”.
data class UrlComponents(val protocol: String, val domain: String, val path: String, val queryParams: Map<String, String>)

more insights

Uncategorized
Jarosław Michalik

3 things that defined my Android dev career

Building an Android developer career isn’t just about coding—it’s about the experiences that push you to grow. Whether it’s connecting with other Android devs at meetups or mastering the fundamentals, it’s those key moments that shape how you approach the craft.

Read More »

AndroidPro newsletter 🚀

join 3057 developers reading AndroidPro newsletter every week

👉 tips & tricks, articles, videos

👉 every Thursday in your inbox

🎁 bonus on signup – Clean Architecture blueprint

brought to you by Jarek Michalik, Kotlin GDE

You can unsubscribe any time. For details visit our privacy policy.

android pro newsletter tips and tricks how to learn android