Sort the map entries by value and show the smallest key.
Acceptance criteria
- 1.#out should show key a.
DSA Tutorial · Hash Maps and Sets
These 10 tasks go with the When a Hash Map Is the Wrong Choice chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Reaching for a Map by reflex is the second most common mistake after nested loops. It is fast at one thing — exact lookup — and unremarkable at everything else.” .
1.Maps lose ordering by valueexercise · easy · 10 min
Sort the map entries by value and show the smallest key.
Passes when: #out should show key a.
2.Range needs sorted dataexercise · easy · 10 min · practice plan
Report how many values in a sorted array fall between 3 and 7 inclusive.
Passes when: #out should show three in range.
3.Array beats map for tiny dataexercise · easy · 10 min · practice plan
Search a two-element array and report hit or miss.
Passes when: #out should show hit.
4.Insertion order is keptexercise · easy · 10 min · practice plan
Show the first key inserted into a Map.
Passes when: #out should show first is z.
5.Find the minimumexercise · medium · 10 min · practice plan
Report the smallest value stored in a Map.
Passes when: #out should show min 1.
6.Fix sorting the map directlybug fix · easy · 8 min
A Map has no sort method, so nothing is ordered.
Passes when: #out should show key a.
7.Fix the range scan on a mapbug fix · medium · 8 min · practice plan
Keys are unordered, so a map cannot answer a range question directly.
Passes when: #out should show three in range.
8.Fix the default sort on numbersbug fix · easy · 8 min · practice plan
sort compares as strings unless given a comparator.
Passes when: #out should show min 3.
9.Fix size read as lengthbug fix · easy · 8 min · practice plan
A Map exposes size, so length is undefined.
Passes when: #out should show holds 2.
10.Fix values spread from a Mapbug fix · easy · 8 min · practice plan
Spreading the Map yields entry pairs, not plain values.
Passes when: #out should show min 1.
Between them these tasks cover Ordering, Range queries and Memory cost.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.