Prepin
Log in

Create an algorithm for a radio station to select the next song to play from a database containing over 1 million songs. You are limited to gathering just one input from the user on the landing page.

Approach / Explanation

This question essentially requires you to create a solution for selecting the next song to be played on a radio station. The task involves managing a database of more than 1 million songs, and you are limited to receiving just one input from the user. It's important to consider how to navigate such a vast database and ensure that the algorithm you develop can perform the task effectively. Key considerations: Comprehend the problem context: Gain an understanding of radio stations' expectations and their song selection process. Also, take into account potential limitations, such as avoiding song repeats within a short period. Identify the single user input: Since you can only obtain one piece of input from the user, determine what type of input would be most beneficial for the algorithm. This could include a preferred genre, favorite artist, mood, etc. Select an appropriate algorithm: Your algorithm must be capable of managing a large database, offering variety, while also ensuring some level of consistency or relevance based on the user input. Focus on optimization: Make sure your algorithm operates efficiently. Avoid situations where the system has to search through the entire collection every time it selects a song.

Suggested Answer

I would select the user’s "current mood" as the input. Depending on the mood they choose, we could filter the database to find songs labeled with that specific mood. A variation of the Randomized Selection Algorithm could be employed for this purpose. Initially, the algorithm would randomly select and play a song from the filtered list. To prevent playing the same song multiple times, we must track the songs that have already been played. A HashMap can be utilized to temporarily store these songs. After a song is played, it is added to the HashMap, which excludes it from the random selection process. Once a certain percentage of songs have been played (for example, 80%), we can clear the HashMap to reintroduce previously played songs into the selection pool. For improved efficiency, we can implement a balanced binary search tree instead of searching through the entire filtered list for the next song. When we choose a song, we can remove it from the tree, which will automatically update our pool of potential next songs.

Alternative Answer

I would prefer to use the user's favorite genre as the input on the landing page. This input will allow us to filter the database to include only those songs that belong to that specific genre. A suitable approach to select the next song could involve utilizing a Queue Data structure in conjunction with a Timer. With this approach, after gathering all the songs from that genre, we can insert them into our queue in a random sequence. We could employ a pseudo-random number generator to arrange the song list. Once the queue is filled, the songs will be played in the order they are removed from the queue. Additionally, we have a timer configured to dequeue a song when it finishes, ensuring a smooth transition between songs. To avoid repeating the same song queue after one cycle, we will reshuffle our list and refill the queue once it is empty. Importantly, we make sure our queue is sufficiently large to provide a continuous stream of songs, yet not so large that it hinders quick adjustments to the user's genre preferences.

Question Details

Difficulty & Category

Medium
technical

Companies

Google

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.