Rearrange Songs by Author
Reported by candidates from Zomato's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Zomato reportedly put this one in front of candidates in July 2026, and the whole thing hinges on a max-heap keyed by remaining song count. You get a list of [author, song] rows and have to reorder them so no two neighbors share an author. The twist is the deterministic tie-breaking, which means a greedy heap pass is the intended answer, not a loose "any valid order" solution. If you've got the OA in a day or two, learn this shape now. StealthCoder sits invisibly on your screen as a safety net if the heap logic slips away mid-assessment.
The problem
You are given a 2D string array songs. Each row is [author, song], and every row identifies one distinct song record. Rearrange all records so that no two consecutive records have the same author. At each position, choose the eligible author with the largest number of unplaced songs. If eligible authors have equal remaining counts, choose the lexicographically smaller author. Within one author, emit songs in lexicographic order. Return the complete rearranged 2D array. If no valid complete rearrangement exists, return an empty 2D array. Function rearrangeSongs(songs: String[][]) → String[][] Examples Example 1 songs = [["A","alpha"],["A","amber"],["B","beta"],["C","coda"]] return = [["A","alpha"],["B","beta"],["A","amber"],["C","coda"]] Author A starts with two songs, so it is selected first. Authors B and C then tie with one song; B wins lexicographically. The remaining records alternate without equal adjacent authors. Example 2 songs = [["A","one"],["A","two"],["A","three"],["B","solo"]] return = [] After placing two records from author A around the only B record, two A records would still need to be adjacent. A complete valid arrangement is impossible. Constraints 1 <= songs.length <= 200000 songs[i].length == 2 1 <= songs[i][0].length, songs[i][1].length <= 100 Every row is a distinct [author, song] pair.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Group songs by author, sort each author's songs lexicographically, then push (count, author) into a heap ordered by larger count first, then smaller author name. Each step, pop the top. If it matches the last placed author, you need the next entry instead, so pop a second one, place from it, then push the first one back. If the heap is empty at that point, return an empty array. The classic pitfall is forgetting to reinsert the skipped author, or reinserting with a stale count. Another is comparing counts but not breaking ties by name. With up to 200000 rows, sort once per author and use pointers, not repeated list removals. Total cost is O(n log k) for k authors. StealthCoder is your hedge in the live OA if the pop-two-and-push-back dance escapes you under pressure.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Rearrange Songs by Author cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Zomato's OA.
Zomato reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Rearrange Songs by Author FAQ
What's the trick in Rearrange Songs by Author?+
Use a max-heap of authors ordered by remaining count, then by lexicographically smaller name. Pop the best author that isn't the last one placed. If the top equals the previous author, take the next one and push the first back. Empty heap with songs left means return an empty array.
How hard is this Zomato OA question really?+
Medium. It's the reorganize-string idea with a string-sorting twist and strict tie-breaking rules. If you've seen task scheduling or reorganize string, it clicks fast. The traps are the tie-break order and handling the skipped author correctly.
How do I detect that no valid arrangement exists?+
Two ways. Upfront, if any author's count exceeds (n + 1) / 2, it's impossible. Or discover it in the loop: when the only author left in the heap equals the last placed author, return an empty array. Both agree, but the in-loop check is safer with the specific greedy rules.
How should I order songs within an author?+
Sort each author's songs lexicographically once, up front, and keep an index pointer per author. Each time you pick that author, emit the song at the pointer and advance it. Don't remove from the front of a list repeatedly, that gets slow at 200000 rows.
How do I prepare in 48 hours for this pattern?+
Write the heap loop from scratch twice. Focus on custom comparators for count descending then name ascending, and the pop-second-then-reinsert step. Test with Example 2 style inputs where one author dominates. That covers nearly every failure case this problem has.