Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Most Frequent Substring

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

Your question is Most Frequent Substring. 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

HubSpot Content Hub can scan text to identify repeated fixed-length fragments for content-quality analysis. Given a lowercase string and a substring length, return the most frequent substring.

If multiple substrings have the same highest frequency, return the one whose first occurrence has the smallest index. Each occurrence is counted by its starting position, including overlapping occurrences.

Formal Specification

Implement find_most_frequent_substring(text, k).

  • Input: text, a non-empty lowercase string, and k, a positive integer.
  • Output: The length-k substring with the highest number of occurrences. The output must be a string of length k.

Constraints

  • 1 <= k <= len(text) <= 100,000
  • text contains only lowercase English letters
  • Overlapping occurrences must be counted
  • Ties are resolved by earliest first occurrence

Function Signature

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