Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions q1.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,31 @@
"""


def first_stable_character(s):
"""
Find the first stable character in the string.
A character is stable if:
1. It appears at least twice
2. All occurrences are in one continuous group
Args:
s (str): Input string
Returns:
str or None: First stable character, or None if no stable character exists
Examples:
>>> first_stable_character("abccba")
'c'
>>> first_stable_character("abc")
None
>>> first_stable_character("a")
None
"""
# TODO: Implement your solution here
pass
def first_stable_character(s: str):
if not s:
return None

freq = {}
for ch in s:
freq[ch] = freq.get(ch, 0) + 1

i = 0
n = len(s)

while i < n:
current_char = s[i]
block_length = 0

while i + block_length < n and s[i + block_length] == current_char:
block_length += 1

if freq[current_char] == block_length:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

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 is None. You need to add a check to ensure the character count is greater than one.

Suggested change
if freq[current_char] == block_length:
if freq[current_char] == block_length and block_length > 1:

return current_char

i += block_length

return None



if __name__ == "__main__":
Expand Down
29 changes: 8 additions & 21 deletions q2.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,16 @@
"""


def compressed_stack_length(lst):
"""
Calculate the number of elements remaining after cancellations.
def compressed_stack_length(arr):
stack = []

Process the list from left to right. When a number matches the most recent
active number, both are removed. Otherwise, the number is added.
for element in arr:
if stack and stack[-1] == element:
stack.pop()
else:
stack.append(element)

Args:
lst (list): List of integers

Returns:
int: Count of numbers remaining after all cancellations

Examples:
>>> compressed_stack_length([1, 2, 2, 3])
2
>>> compressed_stack_length([4, 4, 4, 4])
0
>>> compressed_stack_length([])
0
"""
# TODO: Implement your solution here
pass
return len(stack)


if __name__ == "__main__":
Expand Down
45 changes: 24 additions & 21 deletions q3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The default values for threshold and window do not match the problem's requirements of "3 or more events within a 10-second window". The defaults should be 3 for the event threshold and 10 for the time window.

Suggested change
def find_overloaded_users(events, threshold=10, window=60):
def find_overloaded_users(events, threshold=3, window=10):

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There are two logic errors here that cause incorrect behavior based on the problem description:

  1. The window check > window on line 65 should be >= window. The problem asks for events within a 10-second window (i.e., duration < 10). Your current logic incorrectly includes events with a duration of exactly 10 seconds.
  2. The event count check > threshold on line 68 is incorrect for a "3 or more" events condition. It should be >= threshold to correctly include cases with exactly threshold number of events.
Suggested change
while times[right] - times[left] > window:
left += 1
if right - left + 1 > threshold:
while times[right] - times[left] >= window:
left += 1
if right - left + 1 >= threshold:

overloaded_users.append(user_id)
break

return sorted(overloaded_users)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The problem description specifies that the function should return a set of user IDs. The current implementation returns a sorted list.

Suggested change
return sorted(overloaded_users)
return set(overloaded_users)




if __name__ == "__main__":
Expand Down