Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top N From Logs

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

Your question is Top N From Logs. 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 collection of log entries as input, compute and output the top N items.

Asked in the Phone Interview stage. Coding problem during Generative AI / Java backend AI Engineer phone interview.

Implement a function that takes a list of log entry strings and an integer n, and returns the n most frequent entries. If two entries have the same frequency, return them in lexicographic order.

Function Input

  • logs: list of strings
  • n: integer

Function Output

  • A list of strings containing the top n items in the required order

Constraints

  • 1 <= len(logs) <= 10^5
  • 1 <= n <= len(set(logs))
  • Each log entry is a non-empty string
  • Log entries may repeat many times

Function Signature

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