Technology

C++ STL Algo Quiz

By RougeNeuron

Aug, 2022

Rules

You may search the web, talk a walk down the memory lane or find a new way to get the answer, as long as you choose to learn its all fair!

Assumptions

You are working on an audio player application like Spotify. Assume similar features and services.

1

Assumptions

Your feature generates an overall playlist rating based on the songs added to the playlist.

2

Assumptions

The rating of a song may change at any time. So your component doesn't have to worry about updates or the event reaching the function.

3

Assumptions

Your function applies the following rules and generates the rating. – No song in the playlist should have an explicit rating. – Even if a single piece is unrated, throw an exception.

4

Pseudo Code

Rating getRating (collection_of_songs<Song>) The function accepts a collection of Song objects and return the overall rating.

5

Question

Which of the following algorithms can you use in the getRating function? A. std::any_of B. std::for_each_n C. std::mismatch

?

02

Often we rush to write a for loop when the answer is already available in the STL! Remember, do it once do it right.

TAke Your Time

The Answer

Using std::any_of  with checks for explicit and unrated in two parses will be the most straightforward implementation. Further optimizations later are possible.