Split by rating: hints, no solution

Medium · Linked list

Listeners rate tracks, and a playlist tool wants the low-rated ones surfaced first for review. You are given a queue as an array of ratings from head to tail, and a threshold.

Reorder the queue so that every rating strictly below the threshold comes first, followed by every rating that is at or above it. Inside each of the two groups, the ratings must keep the same relative order they had in the original queue. Return the reordered queue. Ratings may be negative and may repeat.

A linked-list approach builds two chains and then joins them, rather than swapping values.

Examples

Input
ratings = [7, 2, 9, 4, 1], threshold = 5
Output
[2, 4, 1, 7, 9]
Note
Ratings 2, 4 and 1 are below 5 and keep their order, then 7 and 9 follow.
Input
ratings = [3, 3, 3], threshold = 3
Output
[3, 3, 3]
Note
Nothing is strictly below 3, so the queue is unchanged.

Constraints

  • 0 <= ratings.length <= 600
  • -500 <= ratings[i], threshold <= 1000

Hint 1: a nudge

Where should a rating end up if you only look at it and the threshold, ignoring every other rating?

Unlock the full hints and timed practice with a pass

The second and third hints and the timed, runner-checked practice are part of a pass.

See pricing

Sign in

Check my resume first