Problem · Array
Stores and Houses
Learn this problemProblem statement
You are given 2 arrays representing integer locations of stores and houses
(each location in this problem is one-dimensional). For each house, find the
store closest to it. Return an integer array result where result[i] should
denote the location of the store closest to the i-th house. If many stores
are equidistant from a particular house, choose the store with the smallest
numerical location. Note that there may be multiple stores and houses at the
same location.
Function
findClosestStore(houses: int[], stores: int[]) → int[]Examples
Example 1
houses = [5, 10, 17]stores = [1, 5, 20, 11, 16]return = [5, 11, 16]The closest store to the house at location 5 is the store at the same location.
The closest store to the house at location 10 is the store at the location 11.
The closest store to the house at location 17 is the store at the location 16.
Example 2
houses = [2, 4, 2]stores = [5, 1, 2, 3]return = [2, 3, 2]No explanation 👉🥹👈
Example 3
houses = [4, 8, 1, 1]stores = [5, 3, 1, 2, 6]return = [3, 6, 1, 1]No explanation 👉🥹👈
Constraints
Unknown yet. If you happen to know about it, feel free to lmk! TYSM ~3~More Google problems
- Deduplicate Logs: Keep FirstONSITE INTERVIEW · Seen Jul 2026
- Deduplicate Logs: Keep LatestONSITE INTERVIEW · Seen Jul 2026
- Find a Template Across Binary-Tree LeavesONSITE INTERVIEW · Seen Jul 2026
- Maximum Programmer-Problem MatchingONSITE INTERVIEW · Seen Jul 2026
- Minimum Direction ViolationsONSITE INTERVIEW · Seen Jul 2026
- Stream Latest Log VersionsONSITE INTERVIEW · Seen Jul 2026
- Stream Unique Logs in Timestamp OrderONSITE INTERVIEW · Seen Jul 2026
- Top-K IP Addresses from File RecordsONSITE INTERVIEW · Seen Jul 2026