Most Visited Hotel in a Time Window
Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Motive OA reported in July 2022 looks like a hotel problem, but it's really a filter plus a distinct count. Filter visits to the time window, count unique users per hotel, pick the max, break ties alphabetically. If you've got an OA invite for this one, it's a hash map of sets and a single pass. No tricks in the algorithm. The traps are in the details: repeated visits by the same user, inclusive bounds, and an empty window. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but this one is very doable on your own.
The problem
Each index describes one hotel visit by userIds[i] to hotels[i] at integer timestamp timestamps[i]. Among visits whose timestamp is in the inclusive interval [startTime, endTime], return the hotel visited by the largest number of distinct users. Break a tie by returning the lexicographically smallest hotel name. Return the empty string when the interval contains no visit. Function mostVisitedHotel(userIds: String[], hotels: String[], timestamps: int[], startTime: int, endTime: int) → String Examples Example 1 userIds = ["u1","u2","u1","u3"] hotels = ["A","A","A","B"] timestamps = [10,20,25,30] startTime = 10 endTime = 30 return = "A" Hotel A has two distinct users; repeated visits by u1 count once. Example 2 userIds = ["u1","u2"] hotels = ["Beta","Alpha"] timestamps = [5,6] startTime = 0 endTime = 10 return = "Alpha" Both hotels have one user, so the lexicographically smaller name wins. Constraints All three arrays have the same length from 0 through 100000. User IDs and hotel names are non-empty ASCII strings. -1000000000 <= timestamps[i], startTime, endTime <= 1000000000 and startTime <= endTime.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Walk the arrays once. For each index where startTime <= timestamps[i] <= endTime, add userIds[i] to a set stored under hotels[i] in a hash map. After the pass, scan the map and track the best hotel by set size, replacing it when the size is larger or when the size ties and the name is lexicographically smaller. If the map is empty, return the empty string. That's O(n) time plus the final scan. The common pitfall is counting visits instead of distinct users, which breaks Example 1 where u1 visits A twice. Another is using an exclusive bound on the interval. Also watch the tie-break: compare strings directly, not by length. With up to 100000 entries, don't sort every hotel's users. If you freeze during the live OA, StealthCoder can hand you this hash-map-of-sets shape fast, but you should be able to write it from memory.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Most Visited Hotel in a Time Window cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Motive's OA.
Motive reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Most Visited Hotel in a Time Window FAQ
What's the trick in the Motive most visited hotel problem?+
Count distinct users, not visits. Use a map from hotel to a set of user IDs, only after filtering by the inclusive time window. The answer is the hotel with the largest set, with ties broken by the smaller name. That's the whole problem.
How hard is this OA really?+
Easy to medium. There's no clever algorithm, just a hash map of sets and careful edge cases. Most people who miss it either count repeat visits or forget the empty-window case. If you can write a group-by in code, you can solve it.
What edge cases should I test before submitting?+
Test an empty input, a window that contains no visits, and a user visiting the same hotel several times. Also test timestamps exactly equal to startTime and endTime, since both are inclusive. Finally, test a tie between two hotels to confirm the lexicographically smaller name wins.
What's the time complexity and will it pass at 100000 entries?+
A single pass with set inserts is O(n) on average, and the final scan over hotels is at most O(n) comparisons. Strings are compared only during tie-breaks. At 100000 entries this runs comfortably. Avoid nested loops over users and hotels.
How do I prepare for this in 48 hours?+
Practice two or three group-by-with-distinct-count problems using hash maps and sets. Then write this one cold, including the tie-break and empty return. Run both examples by hand. That's enough, since the pattern is simple and reusable.