Start learning free

LESSON 131 OF 291 · DICTIONARIES - CHALLENGES

Phone Book Lookup in Kotlin: Reading a Map Value with !!

Practice Read the full explanation

Given a phone book phoneBook, a HashMap from names to phone numbers, and a name, return the phone number of name.

Every name in the phone book is a key, and its value is that person's phone number, a string like "555-0142". The name you look up is always in the phone book.

 

Example 1:

Input: phoneBook = {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"}, name = "Ben"
Output: "555-0199"
Explanation: The key "Ben" maps to "555-0199".

Example 2:

Input: phoneBook = {"Max": "555-0100"}, name = "Max"
Output: "555-0100"
Explanation: The phone book has a single entry, and it is the one we look up.

Example 3:

Input: phoneBook = {"Tom": "555-0111", "Tim": "555-0111"}, name = "Tim"
Output: "555-0111"
Explanation: Tom and Tim share a phone number. We only need the value of the key "Tim".

 

Constraints:

  • phoneBook has at least one entry.
  • name is always a key of phoneBook.
  • Names are single words made of English letters, and they are all different. Two people can share a phone number.
  • Every phone number is a string of three digits, a dash and four digits, like "555-0142".
STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · DICTIONARIES - CHALLENGES

    Phone Book Lookup in Kotlin, explained

    Problem overview

    The function phoneBookLookup receives phoneBook, a map from names to phone numbers, and a name. It returns the phone number stored under that name. The name is always a key of the map, and numbers are strings like "555-0142".

    With {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"} and name = "Ben", the answer is "555-0199". Two people can share a number: with {"Tom": "555-0111", "Tim": "555-0111"} and "Tim", the answer is "555-0111". Only the key is used to look up the value.

    Approach

    A map answers "what is stored under this key?" directly, with no loop. In Kotlin you read it with square brackets: phoneBook[name].

    The catch is the type. The map might not have the key, so the brackets give a String?, a string that can be null. The function must return a plain String, and Kotlin won't convert one into the other on its own. The lesson says the name is always present, so the not-null assertion !! fits: it tells the compiler "this isn't null" and gives you a String.

    Step by step

    1. Read the value with square brackets using name as the key.
    2. Add !! to turn the String? into a String, then return it.

    The solution in Kotlin

    class Solution {
        fun phoneBookLookup(phoneBook: MutableMap<String, String>, name: String): String {
            return phoneBook[name]!!
        }
    }
    

    Some details worth knowing:

    • Brackets on a map call get. phoneBook[name] is the same as phoneBook.get(name), and for a missing key it returns null. For a map with only "Ana" in it, pb["Zed"] prints null.
    • !! checks at run time. If the value is null, !! throws a NullPointerException. Calling the function with "Zed" on that map throws it. That is acceptable here only because the lesson guarantees the key is present.
    • The map type. MutableMap<String, String> is a map that can be changed. Reading works the same on a read-only Map.

    Other ways to read a value

    Kotlin has a few more ways to read a key, and they differ in what they do for a missing one:

    • map[key] returns null.
    • map.getValue(key) returns a plain String here, and throws a NoSuchElementException for a missing key, with the message Key Zed is missing in the map. for the key "Zed".
    • map[key] ?: "unknown" supplies a replacement when the value is null.

    When a missing key is a bug, !! or getValue fails loudly, which is often what you want. When it is a normal case, give it a default instead. The next lessons on maps deal with that case.

    Complexity

    Time is O(1) on average: a hash map finds a key without scanning the other entries, so a phone book with a million names answers as fast as one with three. Space is O(1) extra, because the function stores nothing new.

    Edge cases and common mistakes

    • Leaving out !!. return phoneBook[name] doesn't compile, because a String? can't be returned where a String is declared.
    • Searching the values. The lookup goes from key to value. Equal numbers under different names don't matter.
    • Different spelling. Keys are compared exactly, so "ben" is not "Ben". The lesson only asks about names that are present.

    Testing

    • {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"} with "Ben" gives 555-0199.
    • {"Max": "555-0100"} with "Max" gives 555-0100.
    • {"Tom": "555-0111", "Tim": "555-0111"} with "Tim" gives 555-0111.
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free