FastPrepMost Visited Hotel in a Time Window

Most Visited Hotel in a Time Window

Motive logoMotive● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

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 = 10endTime = 30return = "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 = 0endTime = 10return = "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.

More Motive problems

See Motive hiring insights
public String mostVisitedHotel(String[] userIds, String[] hotels, int[] timestamps, int startTime, int endTime) {
    // write your code here
}
userIds["u1","u2","u1","u3"]
hotels["A","A","A","B"]
timestamps[10,20,25,30]
startTime10
endTime30
expected"A"
Checking account…