The Nearest Value Mapper
A medium Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L5
Problem
Given keys (list of integers) and values (list of integers), return a dict mapping each key to the value whose numeric distance is smallest. Distance is abs(key - value). When a key is equidistant from two values, break the tie by choosing the smaller value. Duplicate keys are allowed; the last occurrence wins. The returned dict uses STRING keys (e.g. key 5 becomes "5") for JSON compatibility in the test harness, with the chosen integer value.
Summary
Close enough counts. Ties go low.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.