DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Heap

Top K Frequent Elements

**Problem Statement:** Given the required input arguments, write an efficient algorithm to solve the **Top K Frequent Elements** problem. Implement the required logic as specified by standard definitions for this classic algorithmic challenge. **Hint / Expected Approach:** Bucket sort or min-heap of size k **Edge Cases to Consider:** - (1) k = number of unique elements - (2) All elements unique - (3) All same frequency

Examples

Example 1
Input: [1,1,1,2,2,3], 2
Output: [1,2]
Example 2
Input: [1], 1
Output: [1]
Explanation: Single
Example 3
Input: [1,1,2,2,3], 2
Output: [1,2]
Explanation: Tie resolved

Constraints

  • ▪Input arguments are within valid ranges
  • ▪Optimize for execution speed
  • ▪Handle null/empty inputs gracefully

Watch Out For Edge Cases

  • ▪k = number of unique elements
  • ▪All elements unique
  • ▪All same frequency
Frequently Asked At
AmazonGoogleMicrosoftMetaAdobeFlipkart