Practice
1. You need to find the smallest positive number that is divisible by two given integers, and also find the greatest integer that divides both. Which approach guarantees the most efficient and reliable solution for this problem?
easy
Solution
Step 1: Understand the problem requirements
The problem requires finding the greatest common divisor (GCD) and least common multiple (LCM) of two integers efficiently.Step 2: Evaluate approaches
Brute force is inefficient for large inputs. Dynamic programming and greedy approaches do not naturally fit this problem. The Euclidean algorithm efficiently computes GCD in O(log(min(a,b))) time, and LCM can be derived from GCD.Final Answer:
Option C -> Option CQuick Check:
Euclidean algorithm is the standard optimal method for GCD and LCM [OK]
Hint: Euclidean algorithm efficiently finds GCD and LCM [OK]
Common Mistakes:
- Thinking brute force is efficient enough
- Confusing greedy with Euclidean algorithm
2. Consider the following code snippet that checks if a number is a palindrome by reversing half of its digits. What is the value of
reversed_half after the second iteration of the while loop when input is 12321?easy
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]
Hint: Track variable updates carefully each loop iteration [OK]
Common Mistakes:
- Off-by-one iteration count
- Mixing x and reversed_half values
- Incorrect modulo or division
3. You need to find the nth number whose prime factors only include 2, 3, and 5. Which approach guarantees an optimal solution that efficiently generates these numbers without checking every integer sequentially?
easy
Solution
Step 1: Understand the problem constraints
The problem requires generating numbers whose prime factors are only 2, 3, and 5, known as ugly numbers.Step 2: Evaluate approaches for efficiency and correctness
Brute force is too slow; min-heap approach is efficient but can be optimized further; dynamic programming with a 2D table is overcomplicated. The greedy approach with three pointers picks the smallest next multiple of 2, 3, or 5 from previously found numbers, efficiently generating the sequence without duplicates.Final Answer:
Option B -> Option BQuick Check:
Three-pointer greedy approach efficiently generates ugly numbers in ascending order without duplicates [OK]
Hint: Three-pointer greedy approach avoids duplicates and generates numbers in order [OK]
Common Mistakes:
- Assuming min-heap is the only efficient approach
- Thinking DP with 2D table is needed for prime factorization
- Believing brute force is efficient enough for large n
4. Consider the following buggy code snippet for converting an Excel column title to a number. Which line contains the subtle bug that causes incorrect results for inputs like "A" or "Z"?
medium
Solution
Step 1: Identify how letter values are computed
The code usesord(ch) - ord('A'), which makes 'A' = 0 instead of 1.Step 2: Understand impact on result
This causes all results to be off by one, e.g., "A" returns 0 instead of 1.Final Answer:
Option C -> Option CQuick Check:
Correct letter values must be 1-based, not zero-based [OK]
Hint: Check if 'A' maps to 1, not 0 [OK]
Common Mistakes:
- Using zero-based indexing for letters
- Miscomputing powers array
- Incorrect accumulation order
5. The following code attempts to determine if a number is happy but contains a subtle bug. Identify the line with the bug.
def isHappy(n: int) -> bool:
def digit_square_sum(num: int) -> int:
total = 0
while num > 0:
digit = num % 10
total += digit * digit
num //= 10
return total
while n != 1:
n = digit_square_sum(n)
return True
medium
Solution
Step 1: Analyze loop termination condition
The loop only terminates when n == 1, but if n falls into an unhappy cycle, it never reaches 1.Step 2: Identify missing cycle detection
Without tracking seen numbers or using a known cycle set, the code loops infinitely for unhappy numbers.Final Answer:
Option B -> Option BQuick Check:
Infinite loop occurs due to missing cycle detection [OK]
Hint: No cycle detection -> infinite loop on unhappy numbers [OK]
Common Mistakes:
- Assuming loop condition suffices
- Ignoring cycle detection necessity
- Misreading return placement
