The K-th Smallest Prime Fraction interview question gives you a sorted array of prime numbers. You can form fractions arr[i] / arr[j] for all . Your goal is to find the smallest fraction among all possible pairs.
Companies like Microsoft and Amazon ask the Prime Fraction coding problem to test your knowledge of Priority Queues and Binary Search on the answer. It’s a test of whether you can find an element in a virtually sorted set of combinations without generating all fractions.
This problem can be solved with a Min-Heap or Binary Search on Value.
arr[0] / arr[j]. Push all (arr[0]/arr[j], 0, j) into a Min-Heap.(val, i, j), push the next candidate (arr[i+1]/arr[j], i+1, j).arr = [1, 2, 3, 5],
{1/5, 1/3, 1/2}.2/5. Heap: {1/3, 2/5, 1/2}.2/3. Heap: {2/5, 1/2, 2/3}.[2, 5].The "Pop and Push Next" heap pattern is essential for problems involving merging multiple sorted sequences (in this case, each denominator defines a sequence). This is a top-tier Heap interview pattern skill.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find K Closest Elements | Medium | Solve | |
| Number of Subsequences That Satisfy the Given Sum Condition | Medium | Solve | |
| The Latest Time to Catch a Bus | Medium | Solve | |
| Count the Number of Fair Pairs | Medium | Solve | |
| Successful Pairs of Spells and Potions | Medium | Solve |