Generate Playlist Orders That Never Play One Artist Twice in a Row

Last updated: November 29, 2026

Quick Overview

Given a small list of songs, each with an artist, return every ordering of the songs in which no two consecutive songs share an artist. Songs are distinct even if they share an artist.

Apple
Coding & Algorithms
Software Engineer
Apple
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a small list of songs, each with an artist, return every ordering of the songs in which no two consecutive songs share an artist. Songs are distinct even if they share an artist.

A backtracking question with an Apple Music shuffle framing. The interviewer checks that you prune at the point of choice rather than generating all n! permutations and filtering, and that you discuss when backtracking is appropriate at all.

What the Interviewer Expects
  • Use backtracking that builds the order one song at a time
  • Prune when the next song's artist equals the previous artist
  • Track used songs with a boolean array
  • Explain worst-case factorial growth and the small-n assumption
  • Discuss a greedy approach for producing just one valid order on large playlists
Key Topics to Cover
backtracking
permutations
pruning
constraint satisfaction
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Only return one valid ordering for a 2,000-song playlist; what algorithm do you use?
  • Count valid orderings without listing them.
  • Treat songs by the same artist as interchangeable and avoid duplicate orderings.
  • Also forbid the same album twice in a row.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list of (title, artist) tuples, n at most about 8. Output: list of orderings, each a list of titles, in the order produced by trying songs in input order. If no valid ordering exists, return ...

High-Level Design

Recursive build(path, last_artist): if the path has all n songs, record a copy. Otherwise try each unused song whose artist differs from last_artist, mark it used, recurse, then unmark. Pruning at...


Submit Your Answer
Markdown supported