Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Reorganize String With PQ
00:00
5 left

Reorganize String With PQ

HardPython

Problem

Micro1's assessment platform needs to reorder characters in a generated access token so repeated characters are separated. Given a lowercase string s and an integer k, return any rearrangement in which identical characters are at least k positions apart. Return an empty string if no valid rearrangement exists.

Formal Specification

Implement reorganize_string(s, k).

  • Input: s, a string of lowercase English letters, and k, a positive integer.
  • Output: A string containing exactly the same characters as s, with every pair of equal characters separated by at least k positions, or "" if impossible.
  • If k == 1, every permutation is valid, so returning s is acceptable.

Use a max-priority queue for characters currently available and a min-priority queue for characters still in cooldown. At each position, release eligible characters, choose the available character with the greatest remaining frequency, and place it in the result.

Constraints

  • 1 <= len(s) <= 10^5
  • s contains only lowercase English letters
  • 1 <= k <= len(s)
  • Any valid rearrangement is accepted

Function Signature

def reorganize_string(s, k):
Interviewer

Your question is Reorganize String With PQ. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.