Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Count Frequent N-grams Efficiently
00:00
5 left

Count Frequent N-grams Efficiently

HardPython

Problem

Tiber Technologies processes large text streams for search and language features. Implement a one-pass algorithm that identifies the most frequent contiguous n-grams while using bounded memory.

Use the Misra-Gries heavy-hitter algorithm. It maintains at most k - 1 candidate n-grams, so its memory usage does not grow with the stream length. Counts returned are candidate estimates, not guaranteed exact counts.

Formal Specification

Implement count_frequent_ngrams(tokens, n, k):

  • tokens is an iterable of strings. It may be a list or a one-pass generator.
  • n is a positive integer specifying the n-gram length.
  • k is an integer greater than 1 specifying the accuracy and candidate capacity.
  • Return a list of at most k - 1 items. Each item is [ngram, estimated_count], where ngram is a list of n strings.
  • Sort results by decreasing estimated count, then lexicographically by the n-gram.

When a new n-gram arrives, increment its candidate count if present. Otherwise, insert it if capacity remains. If the candidate set is full, decrement every candidate and remove entries that reach zero.

Constraints

  • 0 <= number of tokens <= 10^6
  • 1 <= n <= 10^3
  • 2 <= k <= 10^4
  • Every token is a non-empty string
  • tokens may be a one-pass iterator

Function Signature

def count_frequent_ngrams(tokens, n, k):
Interviewer

Your question is Count Frequent N-grams Efficiently. 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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.