-
Notifications
You must be signed in to change notification settings - Fork 61
Completed Q1, Q2 and Q3 solutions #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -47,27 +47,30 @@ | |||||||||||||||||
| """ | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def find_overloaded_users(events): | ||||||||||||||||||
| """ | ||||||||||||||||||
| Identify users with 3+ events within any 10-second window. | ||||||||||||||||||
| Args: | ||||||||||||||||||
| events (list): List of tuples (user_id, timestamp) | ||||||||||||||||||
| where user_id is int and timestamp is int (seconds) | ||||||||||||||||||
| Returns: | ||||||||||||||||||
| set: Set of user_ids that are overloaded | ||||||||||||||||||
| Examples: | ||||||||||||||||||
| >>> find_overloaded_users([(1, 10), (1, 12), (1, 18), (3, 1), (3, 2), (3, 3)]) | ||||||||||||||||||
| {1, 3} | ||||||||||||||||||
| >>> find_overloaded_users([]) | ||||||||||||||||||
| set() | ||||||||||||||||||
| >>> find_overloaded_users([(1, 1), (1, 20), (1, 40)]) | ||||||||||||||||||
| set() | ||||||||||||||||||
| """ | ||||||||||||||||||
| # TODO: Implement your solution here | ||||||||||||||||||
| pass | ||||||||||||||||||
| from collections import defaultdict | ||||||||||||||||||
|
|
||||||||||||||||||
| def find_overloaded_users(events, threshold=10, window=60): | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default values for
Suggested change
|
||||||||||||||||||
| user_events = defaultdict(list) | ||||||||||||||||||
|
|
||||||||||||||||||
| for user_id, time in events: | ||||||||||||||||||
| user_events[user_id].append(time) | ||||||||||||||||||
|
|
||||||||||||||||||
| overloaded_users = [] | ||||||||||||||||||
|
|
||||||||||||||||||
| for user_id, times in user_events.items(): | ||||||||||||||||||
| times.sort() | ||||||||||||||||||
| left = 0 | ||||||||||||||||||
|
|
||||||||||||||||||
| for right in range(len(times)): | ||||||||||||||||||
| while times[right] - times[left] > window: | ||||||||||||||||||
| left += 1 | ||||||||||||||||||
|
|
||||||||||||||||||
| if right - left + 1 > threshold: | ||||||||||||||||||
|
Comment on lines
+65
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two logic errors here that cause incorrect behavior based on the problem description:
Suggested change
|
||||||||||||||||||
| overloaded_users.append(user_id) | ||||||||||||||||||
| break | ||||||||||||||||||
|
|
||||||||||||||||||
| return sorted(overloaded_users) | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||
|
|
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation incorrectly identifies characters that appear only once as stable. The problem description states that a character must appear at least twice to be considered stable. For example, for the input
"abc", the function will return'a', but the expected output isNone. You need to add a check to ensure the character count is greater than one.