Practice
clock.angleClock(3, 30)?
class ClockAngle:
def __init__(self):
self.precomputed = [[0]*60 for _ in range(12)]
for h in range(12):
for m in range(60):
hour_angle = h * 30 + m * 0.5
minute_angle = m * 6
diff = abs(hour_angle - minute_angle)
self.precomputed[h][m] = min(diff, 360 - diff)
def angleClock(self, hour: int, minutes: int) -> float:
return self.precomputed[hour % 12][minutes]
clock = ClockAngle()
print(clock.angleClock(3, 30))
Solution
Step 1: Calculate hour hand angle at 3:30
Hour angle = 3 * 30 + 30 * 0.5 = 90 + 15 = 105 degrees.Step 2: Calculate minute hand angle and difference
Minute angle = 30 * 6 = 180 degrees. Difference = |105 - 180| = 75 degrees. Minimal angle = 75 degrees.Final Answer:
Option C -> Option CQuick Check:
Precomputed table stores minimal angle correctly -> 75 [OK]
- Using only hour * 30 without minute adjustment
- Returning absolute difference without min(., 360 - .)
from collections import defaultdict
import math
def maxPoints(points):
n = len(points)
if n <= 2:
return n
max_points = 1
points.sort()
for i in range(n):
if max_points >= n - i or max_points > n // 2:
break
slopes = defaultdict(int)
duplicates = 0
for j in range(i + 1, n):
dx = points[j][0] - points[i][0]
dy = points[j][1] - points[i][1]
if dx == 0 and dy == 0:
duplicates += 1
continue
gcd = math.gcd(dy, dx)
if gcd != 0:
dy //= gcd
dx //= gcd
if dx < 0:
dx = -dx
dy = -dy
slopes[(dy, dx)] += 1
curr_max = max(slopes.values(), default=0) + duplicates + 1
max_points = max(max_points, curr_max)
return max_points
points = [(1,1), (2,2), (3,3)]
print(maxPoints(points))
What is the output of this code?Solution
Step 1: Trace outer loop with i=0
Points sorted: [(1,1), (2,2), (3,3)]. For i=0, slopes computed with points (2,2) and (3,3) both have slope (1,1) after normalization.Step 2: Calculate max points for i=0
Slopes[(1,1)] = 2, duplicates=0, so curr_max = 2 + 0 + 1 = 3. max_points updated to 3. Early termination triggers next iterations skipped.Final Answer:
Option B -> Option BQuick Check:
All points lie on the same line y=x, so output is 3 [OK]
- Off-by-one in counting duplicates or base point
- Ignoring early termination condition
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 the loops and operations
The algorithm iterates over the input string once, performing constant-time comparisons and arithmetic per character.Step 2: Address common misconceptions
Some candidates mistakenly think the complexity is O(n^2) by assuming nested loops or repeated lookups, but no nested iteration occurs; no sorting or extra data structures are used.Final Answer:
Option A -> Option AQuick Check:
Actual complexity is O(n), but candidates often incorrectly assume O(n^2) [Trap]
- Assuming nested loops
- Confusing auxiliary space with time
- Thinking sorting is involved
Solution
Step 1: Understand new requirement
Negative numbers are palindrome if their absolute values are palindromes.Step 2: Modify algorithm accordingly
Taking absolute value removes sign issue; then half reversal works unchanged.Step 3: Evaluate other options
Removing negative check without abs causes errors; full reversal risks overflow; string conversion uses extra space; digit comparison without reversal is complex.Final Answer:
Option A -> Option AQuick Check:
Absolute value preserves palindrome property and fits optimal approach [OK]
- Removing negative check without abs
- Switching to full reversal causing overflow
- Using string conversion losing O(1) space
