Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Remove Duplicate Characters

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Remove Duplicate Characters. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Spotify metadata pipelines sometimes need to reduce repetitive characters in playlist and track text without changing the original order. Given a string and a non-negative integer max_duplicates, remove excess occurrences so that each character appears at most max_duplicates + 1 times.

The first occurrence is not considered a duplicate. Keep the earliest occurrences and discard later ones once the limit is reached.

Formal Specification

Implement limit_duplicates(s, max_duplicates).

  • Input: s, a string, and max_duplicates, a non-negative integer.
  • Output: a string containing the retained characters in their original order.
  • Character comparisons are case-sensitive, and each Unicode code point is treated as one character.

Constraints

  • 0 <= len(s) <= 10^5
  • 0 <= max_duplicates <= 10^5
  • Input characters may be arbitrary Unicode code points
  • Character comparisons are case-sensitive
  • Retain the earliest occurrences and preserve their order

Function Signature

def limit_duplicates(s, max_duplicates):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output