Brainlag
  1. Library
  2. Challenges
  3. C

Reverse a playlist in place

Flip the order of a playlist array without making a second array, by swapping from both ends towards the middle.

CCore15 min

A playlist is an array of song titles: const char *songs[], each item a pointer to a string. Write reverse_playlist(songs, n) so that afterwards the array holds the same songs in the opposite order. Do it in place: no second array, no malloc. The songs themselves do not change, only which slot points at which title.

C
const char *songs[] = {"Intro", "Neon Rain", "Static", "Outro"};
reverse_playlist(songs, 4);
// songs is now {"Outro", "Static", "Neon Rain", "Intro"}

Constraint

  • Reverse in place, without malloc.

Checks

  1. An even number of songs
  2. Odd, single and empty
exercise_1.c to run
void reverse_playlist(const char **songs, int n) {
}

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.