Bird
Raised Fist0
Interview Prepmath-geometrymediumAmazonGoogleFacebook

Count Primes (Sieve of Eratosthenes)

Choose your preparation mode4 modes available
▶
Steps
setup

Check if n ≤ 2

The algorithm first checks if the input n is less than or equal to 2. Since n=10, it proceeds to initialize the sieve.

💡 This step handles trivial cases quickly, avoiding unnecessary computation for very small inputs.
Line:if n <= 2: return 0
💡 The algorithm only runs the sieve for inputs greater than 2, as there are no primes less than 2.
📊
Count Primes (Sieve of Eratosthenes) - Watch the Algorithm Execute, Step by Step
Watching each marking of multiples and pointer movement reveals how the sieve efficiently eliminates non-primes without checking every number.
Step 1/10
·Active fill★Answer cell
compare
fill_row
0
1
2
3
4
insert
0
1
2
3
4
move_right
0
i
1
2
3
4
compare
0
i
1
2
3
4
move_right
0
i
1
2
3
j
4
insert
0
i
1
2
3
j
4
move_right
0
i
1
2
3
4
move_right
0
1
2
3
4
record
0
1
2
3
4
Result: 4

Key Takeaways

✓ The sieve efficiently skips even numbers by representing only odds, reducing memory and computation.

This optimization is not obvious from code alone but is clear when seeing the array represent only odd numbers.

✓ Marking multiples starts at i*i because smaller multiples are already handled by smaller primes.

This insight explains why the inner loop starts at i*i, which can be confusing without visualization.

✓ The final count adds 1 for prime 2 separately since the array excludes even numbers.

Understanding this separate counting clarifies why the algorithm returns 1 + sum(is_prime).

Practice

(1/5)
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
A. Use a brute force search checking multiples and divisors until the answer is found.
B. Use a greedy algorithm that picks the largest divisor first and tries to build the LCM.
C. Apply the Euclidean algorithm iteratively to find GCD, then compute LCM using the GCD.
D. Use dynamic programming to store intermediate divisors and multiples for both numbers.

Solution

  1. Step 1: Understand the problem requirements

    The problem requires finding the greatest common divisor (GCD) and least common multiple (LCM) of two integers efficiently.
  2. 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.
  3. Final Answer:

    Option C -> Option C
  4. Quick 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. You are given a list of points on a 2D plane. Your task is to find the rectangle with the minimum area such that all four corners are points from the list. Which approach guarantees finding the optimal solution efficiently?
easy
A. Use a hash set to store points and check pairs of points as diagonals to find rectangles.
B. Use dynamic programming to build rectangles from smaller sub-rectangles.
C. Use a greedy approach by sorting points and selecting closest pairs to form rectangles.
D. Use a brute force approach checking all quadruples of points to find rectangles.

Solution

  1. Step 1: Understand the problem constraints

    The problem requires finding rectangles formed by points with minimum area, which involves checking pairs of points as potential diagonals.
  2. Step 2: Evaluate approaches

    Brute force (checking all quadruples) is correct but inefficient (O(n^4)). Greedy and DP approaches do not guarantee correctness or efficiency here. Using a hash set to check existence of complementary points for diagonals is both correct and efficient (O(n^2)).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Hash set + diagonal pairs -> optimal and correct [OK]
Hint: Hash set + diagonal pairs is key [OK]
Common Mistakes:
  • Thinking greedy or DP solves this efficiently
  • Using brute force without pruning
3. What is the time complexity of the iterative Euclidean algorithm used to compute the GCD of two integers a and b, where a ≥ b > 0?
medium
A. O(min(a, b))
B. O(a + b)
C. O(1)
D. O(log(min(a, b)))

Solution

  1. Step 1: Identify the Euclidean algorithm's iteration count

    The Euclidean algorithm reduces the problem size roughly by at least half every two iterations, leading to logarithmic steps in the smaller number.
  2. Step 2: Conclude time complexity

    Therefore, the time complexity is O(log(min(a, b))). Linear or constant time complexities are incorrect.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Euclidean algorithm runs in logarithmic time relative to smaller input [OK]
Hint: Euclidean algorithm halves problem size quickly [OK]
Common Mistakes:
  • Assuming linear time proportional to min(a,b)
  • Confusing with brute force complexity
4. Suppose the Excel column titles can now include lowercase letters (a-z) in addition to uppercase (A-Z), where 'a' corresponds to 1 and 'z' to 26, independent of uppercase letters. How should the algorithm be modified to correctly convert such titles to numbers?
hard
A. Convert all letters to uppercase and use existing logic
B. Treat uppercase letters as base-26 digits and lowercase letters as separate base-26 digits, then sum
C. Use a combined mapping where 'A'-'Z' and 'a'-'z' both map to 1-26, then apply base-26 logic
D. Map uppercase and lowercase letters separately to 1-26 and multiply by 52 instead of 26

Solution

  1. Step 1: Understand the new character set

    Now there are 52 possible characters (A-Z and a-z), each mapping to 1-26 independently.
  2. Step 2: Adjust base and mapping accordingly

    Since there are 52 distinct characters, the base must be 52, and mapping must distinguish uppercase and lowercase letters.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Base must match character set size; 26 is insufficient for mixed case [OK]
Hint: Base equals number of distinct characters [OK]
Common Mistakes:
  • Ignoring case difference
  • Using base 26 for 52 chars
  • Mapping lowercase to same as uppercase
5. Suppose the problem is modified so that points can have negative coordinates and the input size is very large. Which modification to the optimal approach is necessary to maintain efficiency and correctness?
hard
A. Use a hash set as before, since it supports negative coordinates and lookups remain O(1).
B. Replace the hash set with a balanced binary search tree to handle negative coordinates efficiently.
C. Sort points by coordinates and use two pointers to find rectangles, avoiding hash sets.
D. Switch to brute force checking all quadruples since negative coordinates break the hash set approach.

Solution

  1. Step 1: Consider data structure requirements

    Hash sets in Python support negative integers as keys with O(1) average lookup time.
  2. Step 2: Evaluate alternatives

    Replacing with balanced trees adds unnecessary overhead; sorting and two pointers do not guarantee rectangle detection efficiently; brute force is too slow.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Hash set handles negative coords efficiently -> no change needed [OK]
Hint: Hash sets handle negative keys naturally [OK]
Common Mistakes:
  • Thinking hash sets can't handle negative keys
  • Switching to slower data structures unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard