Sort Array by Increasing Frequency
EasyArrays & Hashinghashing
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Given an integer array nums, return its elements sorted by how often each value occurs, least frequent first.
Values that occur the same number of times are ordered by value in **decreasing** order. Every copy of a value stays in the output, grouped together.
Examples
Input: nums = [1,1,2,2,2,3]
Output: [3,1,1,2,2,2]
3 occurs once, 1 twice, 2 three times.
Input: nums = [2,3,1,3,2]
Output: [1,3,3,2,2]
1 occurs once. 2 and 3 both occur twice, so the larger value 3 comes first.
Input: nums = [-1,1,-6,4,5,-6,1,4,1]
Output: [5,-1,4,4,-6,-6,1,1,1]
Once: 5, -1. Twice: 4, -6. Three times: 1.
Constraints
- 1 <= nums.length <= 100
- -100 <= nums[i] <= 100
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.