Magicsheet logo

Rearrange Characters to Make Target String

Easy
25%
Updated 8/1/2025

Asked by 2 Companies

Rearrange Characters to Make Target String

What is this problem about?

The Rearrange Characters to Make Target String problem asks how many copies of a target string can be formed using characters from a given source string. Each source character can be used at most once per copy. This easy coding problem computes the minimum ratio of available/needed for each required character. The hash table, counting, and string interview pattern is demonstrated.

Why is this asked in interviews?

Meta and Amazon ask this as a clean frequency division problem. It tests the ability to compute "how many complete sets can I form" — a pattern that appears in resource allocation, item crafting, and string construction problems.

Algorithmic pattern used

Frequency map + minimum floor division. Count frequencies in source and target. For each character in target: compute source_freq[c] // target_freq[c]. The answer is the minimum of these ratios across all characters in target.

Example explanation

source="ilovecodingandprogramming", target="coding". Count 'c': source=2, target=1 → 2//1=2. Count 'o': source=2, target=1 → 2. Count 'd': source=2, target=1 → 2. Count 'i': source=2, target=1 → 2. Count 'n': source=2, target=1 → 2. Count 'g': source=2, target=1 → 2. Min = 2 copies.

Common mistakes candidates make

  • Dividing target_freq by source_freq (wrong direction).
  • Not handling characters in target not present in source (result is 0).
  • Using floating division instead of integer floor division.
  • Not iterating over unique characters in target.

Interview preparation tip

"How many complete sets can I form" always uses min(available[c] // needed[c]) over all required characters. This pattern is called "rate-limiting" — the limiting character determines the count. Practice: "number of bouquets from flowers," "minimum craft materials needed." The minimum ratio is always the bottleneck.

Similar Questions