Daily iOS
CS fundamentals and algorithms

Rearranging without adjacent duplicates

You have a list of posts for a feed and need to reorder them so that no two posts by the same author appear back to back. What algorithm do you use, and what's the time complexity?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Count posts per author, then run a greedy that places from whichever author has the most remaining, holding the author you just placed out for one turn before it goes back in. O(n log k), where k is the number of authors.

Share a question