Code RoomSort by frequency
MediumPrep Room Coding #780

Sort by frequency

CodingAlgorithms & data structuresMid–Senior~20 min

Given an integer array, return it sorted so that elements appear in increasing order of frequency (how many times they occur). Among elements with the same frequency, the one with the larger value comes first. Every occurrence of a value stays grouped together. The array has length >= 1. For example [2,3,1,3,2] becomes [1,3,3,2,2]: value 1 has frequency 1 so it leads; values 2 and 3 both have frequency 2, and 3 > 2 so the 3s precede the 2s.

Implement
frequency_sort(nums: list[int]) → list[int]
Examples
in[[1,1,2,2,2,3]]out[3,1,1,2,2,2]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 20 min
InputExpectedGot
[[1,1,2,2,2,3]][3,1,1,2,2,2]not run yetsample