Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Find Top K Most Frequently Read Books

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Find Top K Most Frequently Read Books. Start with the requirements on the right.

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.

Problem

Given a massive log file represented as a list of strings, where each string is the title of a book that a user has read, implement a function to find the top K most frequently read books.

The function should return a list of the top K book titles sorted by their frequency in descending order. If two books have the same frequency, return them in alphabetical order.

Constraints

  • 1 <= logs.length <= 10^5
  • Each book title is a non-empty string with at most 100 characters.
  • 1 <= K <= 10^5 and K will not exceed the number of unique books in logs.

Function Signature

def top_k_frequent_books(logs, K):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output