Return a User's Sorted Meeting History Using a Hash Map Index

Quick Overview

Return one user's meeting timestamps in sorted order from a list of user records, or an empty list when the user is missing. The problem tests building a hash map index, sorting each history with repeated values kept, handling empty histories, and explaining when preprocessing pays off.

Return a User's Sorted Meeting History Using a Hash Map Index

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list of user records `users`, where each record is a list of integers. The first element is the `user_id`, and the remaining elements are meeting timestamps representing that user's meeting history. Implement a function `search(user_id, users)` that returns the sorted meeting history for the given `user_id`. If the `user_id` is not found in the records, return an empty list. Optimize the search using a hash map (dictionary) so that lookups are efficient, achieving `O(1)` average-case lookup time after preprocessing. ### Function Signature ```python def search(user_id: int, users: list[list[int]]) -> list[int]: ``` ### Rules - Each record has the form `[user_id, t1, t2, ...]` and always contains at least the `user_id`. A record that holds only the id belongs to a user with no meetings, and the answer for that user is `[]`. - Return the user's timestamps sorted in ascending order. Repeated timestamps are kept, so the output has one entry per meeting. - If no record has the requested `user_id`, return `[]`. ### Constraints - `1 <= len(users) <= 10^5` - `1 <= user_id <= 10^9` - Each user has 0 to 1000 meetings. - Meeting timestamps are positive integers. - All `user_id` values in `users` are unique. ### Examples ```text Input: user_id = 2, users = [[1, 100, 200, 300], [2, 400, 150, 500], [3, 600]] Output: [150, 400, 500] ``` User 2 has meetings at timestamps 400, 150 and 500. After sorting, the result is `[150, 400, 500]`. ```text Input: user_id = 5, users = [[1, 100], [2, 200], [3, 300]] Output: [] ``` User 5 does not exist in the records, so the function returns an empty list. ```text Input: user_id = 7, users = [[7, 300, 120, 300], [9]] Output: [120, 300, 300] ``` User 7 has two meetings at timestamp 300 and one at 120. Both copies of 300 stay in the sorted output. User 9 has no meetings and does not affect the answer.

Overview: Return one user's meeting timestamps in sorted order from a list of user records, or an empty list when the user is missing. The problem tests building a hash map index, sorting each history with repeated values kept, handling empty histories, and explaining when preprocessing pays off.

PracHub logo
General
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of user records users, where each record is a list of integers. The first element is the user_id, and the remaining elements are meeting timestamps representing that user's meeting history.

Implement a function search(user_id, users) that returns the sorted meeting history for the given user_id. If the user_id is not found in the records, return an empty list.

Optimize the search using a hash map (dictionary) so that lookups are efficient, achieving O(1) average-case lookup time after preprocessing.

Function Signature

def search(user_id: int, users: list[list[int]]) -> list[int]:

Rules

  • Each record has the form [user_id, t1, t2, ...] and always contains at least the user_id . A record that holds only the id belongs to a user with no meetings, and the answer for that user is [] .
  • Return the user's timestamps sorted in ascending order. Repeated timestamps are kept, so the output has one entry per meeting.
  • If no record has the requested user_id , return [] .

Constraints

  • 1 <= len(users) <= 10^5
  • 1 <= user_id <= 10^9
  • Each user has 0 to 1000 meetings.
  • Meeting timestamps are positive integers.
  • All user_id values in users are unique.

Examples

Input: user_id = 2, users = [[1, 100, 200, 300], [2, 400, 150, 500], [3, 600]]
Output: [150, 400, 500]

User 2 has meetings at timestamps 400, 150 and 500. After sorting, the result is [150, 400, 500].

Input: user_id = 5, users = [[1, 100], [2, 200], [3, 300]]
Output: []

User 5 does not exist in the records, so the function returns an empty list.

Input: user_id = 7, users = [[7, 300, 120, 300], [9]]
Output: [120, 300, 300]

User 7 has two meetings at timestamp 300 and one at 120. Both copies of 300 stay in the sorted output. User 9 has no meetings and does not affect the answer.