Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Top K Frequent

Python data engineering interview problem. Difficulty: advanced. Pattern: Heaps. About 18 minutes. Part of the Pro drill bank.

Return the k most frequent numbers, sorted for stability. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Implement top_k_frequent(nums: list, k: int) -> list. Return the k most frequent values sorted ascending for stable output.

Requirements

  • Return sorted ascending.

Constraints

  • Frequencies determine membership, sort is for output stability.

Examples

Input: top_k_frequent([1, 1, 1, 2, 2, 3], 2) Output: [1, 2] 1 and 2 are most frequent; sorted.

Topics: lakebench, python, frequency, heap.

More Python interview questions · All interview problems · Learn data engineering

advanced

Top K Frequent

Interview-style drill: Return the k most frequent numbers, sorted for stability.

Implement `top_k_frequent(nums: list, k: int) -> list`. Return the k most frequent values sorted ascending for stable output. Example: `[1, 1, 1, 2, 2, 3], 2` → `[1, 2]`. Keep the harness.