💡 The two time points are at opposite ends of the array: 0 and 1439. The linear difference is 1439, but the circular difference (going through midnight) is 1.
scan
Scan i=0 - first marked bucket found, set first=0, prev=0
Start scan at i=0. seen[0]=True. prev==-1 (no previous) so this is the first marked bucket. Set first=0, prev=0. min_diff unchanged (no adjacent pair yet).
💡 The first marked bucket initializes 'first' (for the circular check at the end) and 'prev' (to compute gap to next marked bucket).
Line:for i in range(1440):
if seen[i]:
if prev != -1:
min_diff = min(min_diff, i - prev)
else:
first = i # first marked bucket
prev = i
💡 We initialize first and prev at the same value. They'll diverge as we find more marked buckets.
scan
Scan i=1..1438 - all False, skip
Positions 1 through 1438 are all False. The scan continues without updating anything. This represents all the minutes between 00:00 and 23:59 that have no time points.
💡 With only 2 time points there's a large gap with no marked buckets. The scan efficiently skips all False positions.
Line:for i in range(1440):
if seen[i]: # False for i=1..1438, skip
💡 The bucket sort approach is O(1440) = O(1) per time point regardless of n. This is why it beats sort-based approaches for large inputs.
scan
Scan i=1439 - second marked bucket: gap = 1439-0 = 1439
After the scan, check the circular gap: the difference from the LAST time point wrapping around midnight to the FIRST time point. circular_diff = 1440 - prev + first = 1440 - 1439 + 0 = 1. min_diff = min(1439, 1) = 1.
💡 The circular gap represents going from 23:59 forward through midnight to 00:00. It's 1 minute. This is smaller than the 1439-minute linear gap, so the answer is 1.
💡 Always check the circular wrap - it's the most commonly missed case. Clock times are circular: after 23:59 comes 00:00, distance = 1 minute.
def findMinDifference(timePoints):
seen = [False] * 1440
for t in timePoints:
h, m = map(int, t.split(':'))
total = h * 60 + m
if seen[total]:
return 0 # duplicate times mean min diff is 0
seen[total] = True
prev = -1
first = -1
min_diff = 1440
for i in range(1440):
if seen[i]:
if prev != -1:
min_diff = min(min_diff, i - prev)
else:
first = i
prev = i
min_diff = min(min_diff, 1440 - prev + first)
return min_diff
# Example usage
if __name__ == '__main__':
print(findMinDifference(["23:59", "00:00"])) # Output: 1
The key insight: instead of sorting the time strings (O(n log n)), we use the fixed range [0,1439] to sort in O(n) using bucket sort. There are only 1440 possible minute values in a day.
Step 1/6
·Active fill★Answer cell
F
0
F
1
...
2
F
3
T
4
marking
T
0
F
1
...
2
F
3
T
4
first
T
0
F
1
...
2
T
3
first
T
0
F
1
...
2
F
3
T
4
first
T
0
...
1
T
2
first
T
0
...
1
T
2
Key Takeaways
✓ Bucket sort on fixed range [0,1439] gives O(n) time vs O(n log n) for comparison sort. When the range is fixed and small, bucket sort wins.
1440 = 24 hours × 60 minutes = total minutes in a day. This fixed range makes bucket sort applicable.
✓ Circular gap = 1440 - last + first. Always check this - it's the most commonly missed edge case for clock problems.
Draw the clock as a circle: the gap between 23:59 and 00:00 going clockwise is only 1 minute.
✓ Duplicate detection is free: if seen[total] is already True when we try to mark it, two times are identical → min difference = 0, return immediately.
Duplicate times mean the minimum difference is 0. The bucket array detects this in O(1).
Practice
(1/5)
1. You are given an array of integers and need to rearrange it so that no two adjacent elements are the same. Which approach guarantees an optimal solution for this problem?
easy
A. Count frequencies, then use a max heap to greedily place the most frequent elements first, ensuring spacing.
B. Use backtracking to generate all permutations and check for valid arrangements.
C. Sort the array and then swap adjacent elements to avoid duplicates.
D. Use dynamic programming to track valid sequences avoiding adjacent duplicates.
Solution
Step 1: Understand problem constraints
The problem requires rearranging elements so no two adjacent are equal, which demands careful placement of frequent elements.
Step 2: Identify approach that handles frequency and spacing
Using a max heap to always pick the most frequent element available and placing it ensures no two identical elements are adjacent, which brute force or simple sorting cannot guarantee efficiently.
Final Answer:
Option A -> Option A
Quick Check:
Max heap approach handles frequency and spacing correctly [OK]
Hint: Max heap ensures spacing of frequent elements [OK]
Common Mistakes:
Thinking sorting alone avoids adjacent duplicates
Assuming DP solves this without frequency tracking
2. You are given an unsorted array of integers and need to find the maximum difference between successive elements in its sorted form, but you want to avoid sorting the entire array explicitly if possible. Which approach guarantees an optimal solution that leverages the problem's constraints?
easy
A. Use a dynamic programming approach to build up maximum gaps incrementally.
B. Apply the pigeonhole principle by distributing numbers into buckets to find the maximum gap between buckets.
C. Use a greedy algorithm that picks the largest and smallest elements repeatedly to find the maximum gap.
D. Sort the array fully and then scan to find the maximum gap between adjacent elements.
Solution
Step 1: Understand the problem constraints
The problem requires finding the maximum gap between sorted adjacent elements without necessarily sorting the entire array explicitly.
Step 2: Identify the approach that leverages problem properties
The pigeonhole principle approach distributes elements into buckets based on min and max values, ensuring the maximum gap lies between buckets, not inside them, enabling O(n) time complexity.
Hint: Maximum gap lies between buckets, not inside [OK]
Common Mistakes:
Assuming sorting is always necessary
Using greedy without sorting or bucketing
Confusing DP with sorting problems
3. Consider this modified h-index code snippet. Which line contains a subtle bug that can cause incorrect h-index results?
medium
A. Line 12: updating low to mid + 1 when count >= mid
B. Line 6: initializing count to zero inside the loop
C. Line 1: sorting citations in ascending order instead of descending
D. Line 9: breaking the loop when c < mid
Solution
Step 1: Identify sorting order impact
Sorting ascending breaks the assumption that citations are in descending order, so the early break in counting fails.
Step 2: Check other lines
Count initialization, break condition, and binary search updates are correct given descending order.
Final Answer:
Option C -> Option C
Quick Check:
Ascending sort causes incorrect early break and wrong counts [OK]
Hint: Descending sort is critical for early break in counting [OK]
Common Mistakes:
Sorting ascending by mistake
Not breaking early
Off-by-one in binary search updates
4. Suppose the input array can contain negative integers and duplicates, and you want to find the maximum gap between successive unique elements in sorted order. Which modification to the bucket sort / pigeonhole approach correctly handles this scenario without sorting the entire array explicitly?
hard
A. Sort the array fully to handle negatives and duplicates, then scan for maximum gap.
B. Use buckets sized to cover only positive numbers and skip negatives during bucketing.
C. Ignore duplicates by using a set before bucketing, then apply the standard bucket approach on unique values.
D. Modify bucket size to be the difference between max and min ignoring duplicates, but keep duplicates in buckets.
Solution
Step 1: Handle duplicates
Duplicates do not affect maximum gap between unique elements, so remove duplicates first.
Step 2: Apply bucket approach on unique values
After deduplication, apply pigeonhole principle buckets over full range including negatives.
Step 3: Avoid sorting entire array
This approach avoids full sorting and correctly handles negatives and duplicates.
Final Answer:
Option C -> Option C
Quick Check:
Deduplication before bucketing preserves correctness [OK]
Hint: Remove duplicates first, then bucket over full range including negatives [OK]
Common Mistakes:
Ignoring negatives in bucket sizing
Keeping duplicates causing incorrect gaps
Sorting entire array defeats bucket approach
5. Suppose the problem is modified so that elements in arr1 can appear multiple times and must be reused unlimited times in the output, respecting arr2 order first, then ascending leftover elements. Which modification to the optimal counting sort approach correctly handles this?
hard
A. Keep frequency counts as is and output elements only once per frequency count
B. Modify frequency array to store maximum allowed reuse counts and output elements repeatedly based on that
C. Remove frequency counting and use a hash map to track counts, then output elements in arr2 order repeated as needed
D. Ignore frequency counts and output elements in arr2 order repeatedly until a fixed output size is reached
Solution
Step 1: Understand reuse requirement
Unlimited reuse means frequency counts must represent maximum allowed repeats, not just input counts.
Step 2: Modify frequency array accordingly
Frequency array should store max reuse counts; output elements repeatedly based on these counts respecting arr2 order.
Step 3: Reject naive approaches
Simply outputting once or ignoring counts breaks correctness; hash map alone doesn't handle unlimited reuse without max counts.
Final Answer:
Option B -> Option B
Quick Check:
Frequency array extended to max reuse counts handles unlimited repeats correctly [OK]
Hint: Unlimited reuse requires frequency counts to represent max repeats [OK]
Common Mistakes:
Outputting elements only once ignoring reuse
Using hash map without max reuse logic
Repeating output arbitrarily without frequency control
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.