Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement Solution.frequencySort(s). Return a permutation of s in which characters with higher frequency appear before characters with lower frequency. Test cases avoid ties between distinct frequencies.

Starter code

class Solution:
    def frequencySort(self, s):
        pass
Test cases

three-one

{
  "args": [
    "tree"
  ]
}

Expected: "eetr"

Wizard outline
  1. Step 1: Initialize Solution.frequencySort

    Replace the empty starter with the first real state owned by Solution.frequencySort. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Assemble the primary transition

    Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.

  3. Step 3: Pass the Single case

    Complete the readable core algorithm for one representative Interview case. Count characters and use frequency-indexed buckets to emit them in descending count order.

  4. Step 4: Harden the Three One boundary

    Repair the reviewed boundary and pass the complete submission contract. Each character is placed in exactly the bucket equal to its frequency. Visiting bucket indices in descending order emits every higher-frequency character first, and repeating by the index preserves exactly all input occurrences.

Footguns and prerequisites
  • Emit each character count times; sorting only the distinct characters loses repeated occurrences.
  • hashing and sets
  • strings
Reviewed references
Practice prerequisites
  • Rank Top Words(opens in a new tab)

    Ranking frequency entries by a deterministic compound key prepares the frequency-first ordering required when sorting characters.

  • Update a Bounded Heap(opens in a new tab)

    Update a Bounded Heap isolates after each value, the heap contains the largest min(k, processed_count) values seen so far. That focused state discipline is required when implementing sort characters by frequency as a complete Interview Problem.

Recommended approach and implementation

Count each character, place characters into a bucket indexed by count, then traverse buckets from the maximum count down and repeat each character by its count.

Why it works: Each character is placed in exactly the bucket equal to its frequency. Visiting bucket indices in descending order emits every higher-frequency character first, and repeating by the index preserves exactly all input occurrences.

class Solution:
    def frequencySort(self, s):
        """
        Checkpoint 1: initialize the state owned by this Interview contract.
        Checkpoint 2: assemble the primary transition without hiding the boundary.
        """
        counts = {}
        for character in s:
            counts[character] = counts.get(character, 0) + 1
        buckets = [[] for _ in range(len(s) + 1)]
        for character, count in counts.items():
            buckets[count].append(character)
        output = []
        for count in range(len(s), 0, -1):
            for character in buckets[count]:
                output.append(character * count)
        return ''.join(output)
Similar exercises