Practice
isRectangleOverlap([0,0,2,2], [1,1,3,3])?Solution
Step 1: Calculate x_overlap
min(2,3) - max(0,1) = 2 - 1 = 1 > 0Step 2: Calculate y_overlap
min(2,3) - max(0,1) = 2 - 1 = 1 > 0Final Answer:
Option C -> Option CQuick Check:
Both overlaps positive means rectangles overlap [OK]
- Confusing > 0 with >= 0 causing false positives on touching edges
- Mixing up coordinates leading to negative overlap
- Assuming overlap if only one axis overlaps
nthUglyNumber(4)?
import heapq
def nthUglyNumber(n):
heap = [1]
seen = {1}
ugly = 1
for _ in range(n):
ugly = heapq.heappop(heap)
for factor in [2, 3, 5]:
new_ugly = ugly * factor
if new_ugly not in seen:
seen.add(new_ugly)
heapq.heappush(heap, new_ugly)
return ugly
print(nthUglyNumber(4))
Solution
Step 1: Trace heap pops and pushes for n=4
Initial heap: [1]. Iteration 1 pops 1, pushes 2,3,5. Iteration 2 pops 2, pushes 4,6,10. Iteration 3 pops 3, pushes 6 (already seen), 9, 15. Iteration 4 pops 4, pushes 8,12,20.Step 2: Identify the 4th popped ugly number
The popped numbers in order are 1, 2, 3, 4. The 4th ugly number is 4, so nthUglyNumber(4) returns 4.Final Answer:
Option D -> Option DQuick Check:
4 is the 4th ugly number generated by the heap [OK]
- Confusing the iteration count with the returned value
- Forgetting duplicates are skipped
- Miscounting the order of popped elements
n using an optimized sieve. Identify the line containing the subtle bug that causes incorrect results or inefficiency.
def countPrimes(n):
if n <= 2:
return 0
is_prime = [True] * (n // 2)
is_prime[0] = False # 1 is not prime
for i in range(3, int(n**0.5) + 1, 2):
if is_prime[i // 2]:
for j in range(i, n, 2 * i): # Bug here
is_prime[j // 2] = False
return 1 + sum(is_prime) # add prime 2
Solution
Step 1: Understand inner loop start
The inner loop should start marking multiples from i*i, not i, to avoid redundant marking.Step 2: Identify impact of bug
Starting from i causes marking the prime number i itself as non-prime, corrupting results.Final Answer:
Option C -> Option CQuick Check:
Correct sieve starts marking from i*i to preserve prime i [OK]
- Starting marking from i instead of i*i
- Mislabeling 1 as prime
- Forgetting to add prime 2
Solution
Step 1: Identify operations performed
The algorithm only performs a fixed number of min and max operations on rectangle coordinates.Step 2: Recognize no loops or recursion involved
Since no iteration over points or sorting is done, the time complexity is constant.Final Answer:
Option A -> Option AQuick Check:
Only a few arithmetic and comparison operations -> O(1) [OK]
- Assuming complexity depends on rectangle size or points inside
- Confusing coordinate comparisons with sorting
- Thinking overlap check requires scanning all points
import heapq
def nthUglyNumber(n):
heap = [1]
seen = set()
ugly = 1
for _ in range(n):
ugly = heapq.heappop(heap)
for factor in [2, 3, 5]:
new_ugly = ugly * factor
if new_ugly not in seen:
seen.add(new_ugly)
heapq.heappush(heap, new_ugly)
return ugly
Solution
Step 1: Check initialization of seen set
The seen set is empty initially, but 1 is already in the heap. This causes duplicates because 1 is not marked as seen.Step 2: Understand impact of missing 1 in seen
Without 1 in seen, the algorithm may push 1 multiple times, causing duplicates and incorrect nth ugly number.Final Answer:
Option A -> Option AQuick Check:
Adding 1 to seen fixes duplicates and ensures correctness [OK]
- Forgetting to add initial element to seen set
- Assuming heap uniqueness without seen tracking
- Ignoring edge cases with initial values
