Twitter | Design round-robin collaborative playlist of songs | Rejected

YOE : 17 in java, includes 10 in android.
applied for senior engineer roles, hoping I'd be considered for Staff level roles also.

/**

  • We are building a mobile application that lets you manage music
  • playlists. The most important thing about our app is that users can
  • only progress forward in the playlist. If a song is played fully or
  • skipped, there’s no going back to hear it again.
  • We are adding a feature for users to create collaborative playlists
  • with friends. Each user will independently create their own playlist
  • and our application will interleave songs from provided playlists
  • together as if it were one long playlist. **The app will cycle through
  • each playlist in a “round-robin” fashion, playing the next available
  • song until all provided playlists are empty.**
  • Implement a class called CollaborativePlaylist class that implements
  • the Playlist interface and matches the behavior described above. The
  • constructor of CollaborativePlaylist should accept a List as
  • its source of data.
  • /
interface Playlist {
    @Nullable Song getNextSong();
    boolean hasNextSong();
}

class Song {
    String name;

    public Song(String name) {
        this.name = name;
    }
}

class CollaborativePlaylist implements Playlist {
    public CollaborativePlaylist(@NotNull List<Playlist> playlists) {
	}
	
	@Override
    @Nullable
    public Song getNextSong() {
    }

    @Override
    public boolean hasNextSong() {
    }

My brute-force solution, assuming Playlist.hasNextSong() works correctly to help achieve round-robin shuffle-feature.

class CollaborativePlaylist implements Playlist {

    // Space - O ( N )
    private Deque<Song> songsList;

    public CollaborativePlaylist(@NotNull List<Playlist> playlists) {
        songsList = new LinkedList<>();
        prepareSongsList ( playlists );
    }

    private void prepareSongsList( List<Playlist> playlists ) {
        int index = 0;
        while ( index < playlists.size() ) {
            if ( playlists.get( index ).hasNextSong() ) {
                index = addSong ( playlists, index );
                if ( index == playlists.size() ) {
                    index = 0;
                }
            } else {
                index++;
            }
        }
    }

    private int addSong ( List<Playlist> playlists, int index ) {
        if ( playlists.get( index ).hasNextSong() ) {
            songsList.addFirst ( playlists.get(index).getNextSong() );
        }
        return ++index;
    }

    @Override
    @Nullable
    public Song getNextSong() {
        return songsList.removeLast();
    }

    @Override
    public boolean hasNextSong() {
        return songsList.isEmpty();
    }
}

Outcome: Rejected, clearly I did not provide the most efficient and optimal solution, because I succumbed to anxiety.

Comments (7)