Practice
reversed_half after the second iteration of the while loop when input is 12321?Solution
Step 1: Trace first iteration
Initial x=12321, reversed_half=0 reversed_half = 0*10 + 12321%10 = 1 x = 12321 // 10 = 1232Step 2: Trace second iteration
reversed_half = 1*10 + 1232%10 = 10 + 2 = 12 x = 1232 // 10 = 123Final Answer:
Option A -> Option AQuick Check:
After two iterations reversed_half is 12 [OK]
- Off-by-one iteration count
- Mixing x and reversed_half values
- Incorrect modulo or division
def reverse(x: int) -> int:
INT_MAX = 2**31 - 1
INT_MIN = -2**31
sign = -1 if x < 0 else 1
x = abs(x)
def helper(num, rev):
if num == 0:
return rev
pop = num % 10
if rev > INT_MAX // 10 or (rev == INT_MAX // 10 and pop > 7):
return 0
return helper(num // 10, rev * 10 + pop)
rev = helper(x, 0)
return sign * rev if rev != 0 else 0
print(reverse(120))Solution
Step 1: Trace helper calls for input 120
helper(120,0) -> pop=0, helper(12,0*10+0=0) helper(12,0) -> pop=2, helper(1,0*10+2=2) helper(1,2) -> pop=1, helper(0,2*10+1=21) helper(0,21) returns 21Step 2: Apply sign and check overflow
Input is positive, so final return is 21. No overflow detected.Final Answer:
Option B -> Option BQuick Check:
Reversed 120 is 21 after dropping trailing zero [OK]
- Returning 12 by reversing digits incorrectly
- Returning 0 due to wrong overflow check
- Confusing pop digit order
Solution
Step 1: Identify loops in the optimal approach
The code iterates over all pairs of points (i,j), which is O(n^2).Step 2: Analyze point existence checks
Checking if complementary points exist uses a hash set with O(1) average lookup, so no extra multiplicative factor.Final Answer:
Option B -> Option BQuick Check:
Double loop + O(1) lookup -> O(n^2) time [OK]
- Confusing with brute force O(n^4)
- Assuming linear search inside pairs causes O(n^3)
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
Solution
Step 1: Identify time complexity
Each digit is processed once, so time is O(d) where d is number of digits.Step 2: Identify space complexity
Recursive calls stack up to d levels, so space is O(d) due to recursion stack.Final Answer:
Option A -> Option AQuick Check:
Linear time and recursion stack space matches digit count [OK]
- Assuming constant space ignoring recursion stack
- Confusing digit count with input size n
- Thinking time is quadratic due to multiplication
