As reported: D. E. Shaw, 2026 — a senior Futures-team virtual round (solved with a frequency map and a priority queue, all cases passing) and an SMTS screening, where it was asked verbally: brute force first, then frequency map + max-heap, with the impossible case when a character occurs more than
(n + 1) / 2times; follow-ups on the time complexity ofunordered_mapvsmap, why the ordered map is slower, whenunordered_mapdegrades, and when to use each.
This is LeetCode 767 — Reorganize String. Our write-up: /reorganize-string.
Given a string s, rearrange its characters so that no two adjacent characters are equal. Return any such arrangement, or "" if none exists.
Example 1
s = "aab"
Output: "aba"
Example 2
s = "aaab"
Output: ""
Constraints: 1 ≤ s.length ≤ 500; s has lowercase English letters.
Code runner format: one JSON value per line: s. Implement reorganizeString(s). Any valid arrangement is accepted; the checker prints "ok" for a correct answer.
Follow-ups
- Time complexity of
unordered_mapvsmap(ordered): why is the ordered map's higher, when doesunordered_mapdegrade, and when would you use each?