You're given a list of attendees who have checked in, and a list of names to look up. For each lookup, report whether that name is among the checked-in attendees.
checkIn(attendees, queries)
Return one boolean per query, in order.
Input format: attendees | queries — space-separated names divided by a pipe.
Output the booleans as true/false, space-separated.
checkIn(["ann", "bob"], ["bob", "cat"]) // → [true, false]
checkIn(["x"], ["x", "x"]) // → [true, true]
checkIn([], ["a"]) // → [false]
This problem asks the same question over and over: "is this name among the attendees?" Scanning the whole attendee list for every query works, but it adds up — the total work grows with the number of attendees times the number of queries.
The attendee list never changes across queries — only the name you're asking about changes. Whenever you ask the same kind of question against the same fixed collection many times, pay a one-time setup cost so each individual question becomes fast.
Without the set, q queries against n attendees cost roughly n times q. With the set, building it costs n once, and each of the q queries costs a small constant amount — roughly n plus q. For small inputs the difference is invisible, but as either number grows, the gap between "n times q" and "n plus q" gets enormous.
Duplicate queries are handled naturally — each is answered independently by the set, so repeats just produce the same answer twice. An empty attendee list also just works: the set is empty, and every check correctly comes back false.
def check_in(attendees, queries):
attendee_set = set(attendees)
return [q in attendee_set for q in queries]
Sign in to join the discussion.
No comments yet. Be the first to ask a question.
Press Run to execute your code, or Submit to test and complete this problem.