Curriculum

Curriculum

Handle a Missing Map Key Safely

Scenario

Room map: { "Room-A": 2, "Room-B": 0 }. Look up three room codes.

Selected choicePick a choice below
Result—

Try it yourself

Compare a known open room, a known full room, and a missing room code.

Reveal the explanation
  1. Lookup "Room-A"

    Key exists -> returns 2 free seats (Accept).

  2. Lookup "Room-B"

    Key exists with value 0 -> returns "Full" (NOT "Unknown room").

  3. Lookup "Room-Z" (not in map)

    Key absent -> returns explicit "UNKNOWN_ROOM" status instead of crashing.

Always check whether the key exists in the map first. Returning 0 for a missing room blurs "Room-B is full" with "Room-Z does not exist".

Review first
Download shared test cases

Your turn

Given equipmentStock = { "MIC-1": 0, "CAM-2": 3 }, state the exact return values for "MIC-1", "CAM-2", and "PROJ-9".

Common mistake Returning 0 for "PROJ-9" so a typo looks identical to an out-of-stock microphone.

Check your work

MIC-1 -> 0 (out of stock); CAM-2 -> 3 (available); PROJ-9 -> UNKNOWN_ITEM.

Quick check

Why should lookup("Room-Z") return UNKNOWN_ROOM rather than 0 when "Room-B" has 0 seats?
Show the answer and reasoning

Correct answer So callers can distinguish a real full room from a typo or nonexistent room

Checking key existence keeps a valid zero value distinct from a missing record.