Practice
isHappy(7)?
def sum_of_squares(num):
return sum(int(d) ** 2 for d in str(num))
UNHAPPY_CYCLE = {4, 16, 37, 58, 89, 145, 42, 20}
def isHappy(n: int) -> bool:
while n != 1 and n not in UNHAPPY_CYCLE:
n = sum_of_squares(n)
return n == 1
print(isHappy(7))
Solution
Step 1: Trace sum_of_squares for n=7
7 -> 7² = 49Step 2: Iterate until n=1 or cycle detected
49 -> 4²+9²=16+81=97 -> 9²+7²=81+49=130 -> 1²+3²+0²=1+9+0=10 -> 1²+0²=1 -> loop ends, returns trueFinal Answer:
Option B -> Option BQuick Check:
7 is a happy number, code returns true [OK]
- Assuming 7 leads to cycle or infinite loop
k. What is the returned value when called with nums = [2, 7, 6, 1] and k = 5?
def longest_subarray_div_by_k(nums, k):
earliest = [-2] * k
earliest[0] = -1
max_length = 0
prefix_sum = 0
for i, num in enumerate(nums):
prefix_sum += num
remainder = prefix_sum % k
if remainder < 0:
remainder += k
if earliest[remainder] != -2:
max_length = max(max_length, i - earliest[remainder])
else:
earliest[remainder] = i
return max_length
Solution
Step 1: Trace prefix sums and remainders
nums = [2,7,6,1], k=5 - i=0: prefix_sum=2, remainder=2, earliest[2]=-2 -> earliest[2]=0 - i=1: prefix_sum=9, remainder=4, earliest[4]=-2 -> earliest[4]=1 - i=2: prefix_sum=15, remainder=0, earliest[0]=-1 -> max_length=max(0,2-(-1))=3 - i=3: prefix_sum=16, remainder=1, earliest[1]=-2 -> earliest[1]=3Step 2: Final max_length is 3
The longest subarray divisible by 5 has length 3 (from index 0 to 2).Final Answer:
Option A -> Option AQuick Check:
Longest subarray length = 3 [OK]
- Off-by-one errors in length calculation
- Forgetting to initialize earliest[0] = -1
- Ignoring negative remainder adjustment
k elements that appear most frequently. Which algorithmic approach guarantees an optimal solution with linear time complexity?Solution
Step 1: Understand problem constraints
The problem requires finding top k frequent elements efficiently, ideally in linear time.Step 2: Identify approach with linear time complexity
Bucket sort groups elements by frequency, allowing O(n) time retrieval of top k elements by scanning buckets from high to low frequency.Final Answer:
Option A -> Option AQuick Check:
Bucket sort approach is known to run in O(n) time for this problem [OK]
- Sorting entire array is O(n log n), not optimal
- Greedy approach ignores frequency counts
- DP is unrelated to frequency counting here
Solution
Step 1: Understand reuse requirement
Elements can be reused unlimited times, so counts should not be decremented after matching.Step 2: Evaluate modifications
Two-pointer approach relies on moving pointers forward, so it cannot handle reuse without complex resets. Hash map counting without decrementing counts allows unlimited reuse correctly.Final Answer:
Option A -> Option AQuick Check:
Hash map frequency counting without decrement handles reuse correctly [OK]
- Not changing pointer logic for reuse
- Decrementing counts incorrectly
- Resetting pointers causing infinite loops
Solution
Step 1: Understand the reuse requirement
Allowing reuse means the same element can be chosen multiple times, which breaks the original hash map approach that assumes distinct indices.Step 2: Identify suitable algorithm
Dynamic programming similar to coin change counts combinations allowing reuse, correctly handling multiple uses of the same element.Step 3: Evaluate other options
Original hash map fails reuse; two pointers assume distinct elements; brute force with same index pairs is invalid.Final Answer:
Option C -> Option CQuick Check:
DP approach handles reuse correctly, unlike hash map or two pointers [OK]
- Assuming original hash map works with reuse
- Using two pointers without modification
- Allowing same index pairs in brute force
