Sort Letters by Frequency
Updated
Problem statement
You are given a string s made of lowercase English letters. Reorder its letters by how many times each letter appears:
- A letter that appears more times comes before a letter that appears fewer times.
- If two letters appear the same number of times, the one that comes first in the alphabet goes first.
Keep every copy. A letter that appears k times shows up k times in a row in the answer. Return the reordered string.
Function
sortLettersByFrequency(s: String) → StringExamples
Example 1
s = "banana"return = "aaannb"a appears 3 times, n appears 2 times and b appears once. So the answer is aaa, then nn, then b.
Example 2
s = "mississippi"return = "iiiissssppm"i and s each appear 4 times. They tie, so i goes first because it comes first in the alphabet. Then come p (2 times) and m (1 time).
Example 3
s = "zebra"return = "aberz"Every letter appears once. All counts tie, so the letters go in alphabetical order.
Constraints
1 <= s.length <= 5000sconsists only of lowercase English letters.
More Scale AI Phone Screen questions
- Four-Player Trick-Taking Card Game SimulationOA · PHONE SCREEN · Seen Aug 2026
- Task Processor: Dependencies and DeadlinesPHONE SCREEN · Seen Aug 2026
- Best Poker Category with a JokerPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
- Free Time for Every Meeting RoomPHONE SCREEN · Seen Apr 2026
- Free Windows Between Busy IntervalsOA · PHONE SCREEN · Seen Apr 2026
- Order Queries by Shortest Processing TimePHONE SCREEN · Seen Apr 2026
- Party Time by Town and Quiet Gaps by CityPHONE SCREEN · Seen Jan 2026
- Party Windows and Dead Zone TimePHONE SCREEN · Seen Oct 2025
- Overlap Duration of Two Time IntervalsPHONE SCREEN · Seen Jun 2025
- Multi-Head Attention BlockPHONE SCREEN · Seen Mar 2025
Similar questions at other companies
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Portfolio Valuation, Part 1Stripe · PHONE SCREENStripe interview questions
- Summarize Server Healthcheck LogsNVIDIA · PHONE SCREENNVIDIA interview questions
- Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
- Best Anagram MatchGoogle · PHONE SCREENGoogle interview questions
- Word Frequency in a Large TextRobinhood · PHONE SCREENRobinhood interview questions
- Remove Later Anagrams From a ListBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Employees With Shared InterestsPalantir · PHONE SCREENPalantir interview questions
- Rank Words by FrequencyDatadog · PHONE SCREENDatadog interview questions
- Average Process Time from CSVAbnormal Security · PHONE SCREEN