Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Sorting Movies by Rating
00:00
5 left

Sorting Movies by Rating

EasyPython

Problem

In a test fixture for a NetApp BlueXP media dashboard, movie records must be displayed in a deterministic order. Given a list of movies, sort them by rating descending. Movies with the same rating must be sorted by title ascending, using case-sensitive lexicographic order. If two records have identical ratings and titles, preserve their original relative order.

Return a new sorted list without changing the input list.

Formal Specification

Implement sort_movies(movies), where movies is a list of dictionaries. Each dictionary contains:

  • title: a non-empty string
  • rating: a number from 0 through 10, inclusive

Return a list containing the same movie dictionaries in the required order. Do not create or remove movie records.

Constraints

  • 0 <= len(movies) <= 10^5
  • 0 <= movies[i]["rating"] <= 10
  • Titles are non-empty strings of at most 200 characters
  • The input list must not be modified

Function Signature

def sort_movies(movies):
Interviewer

Your question is Sorting Movies by Rating. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.