AiLearn AI Coding
hardStructured · sandbox60 min Premium

Maximize Unique Characters

Maximize the number of distinct characters by choosing and swapping, greedy plus backtracking with a bug → feature → scale arc.

Greedy / PackingBacktracking

Problem

A string utility counts and maximizes unique characters. The starter code has a bug.

Phase 1 — Fix the bug

uniqueCount(s) miscounts strings with repeated characters: "abca" should be 3, but the buggy count returns 4. Count distinct characters exactly.

Phase 2 — Feature

Implement maximizeUniqueness(s, k): with at most k adjacent swaps, maximize the number of unique characters in the resulting string. Report the maximum count reachable.

Phase 3 — Scale

Strings grow long. A search over all swap sequences won't survive — you need to think about which swaps can possibly matter and prune aggressively.

What interviewers watch

Exact distinct counting, an honest swap-cost model, and pruning — the AI often hand-waves the search into a brute force that times out on phase 3.