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
Optimize Sequential Block Retrieval
00:00
5 left

Optimize Sequential Block Retrieval

MediumPython

Problem

At Nimbus Storage, block requests are processed in order, but the retrieval layer can prefetch a limited number of distinct blocks at a time. Given a sequence of requested block IDs and a cache capacity k, write a function that returns the length of the longest contiguous request segment that can be served using at most k distinct block IDs.

This models optimizing retrieval by maximizing the largest request window that fits within the storage system's active block set.

Formal Specification

  • Input:
    • requests: a list of integers representing block IDs
    • k: an integer representing the maximum number of distinct block IDs allowed in the active retrieval window
  • Output:
    • An integer: the maximum length of a contiguous subarray containing at most k distinct values

Constraints

  • 1 <= requests.length <= 10^5
  • 0 <= requests[i] <= 10^9
  • 0 <= k <= requests.length
  • The answer must be computed in O(n) time

Function Signature

def longest_retrieval_window(requests, k):
Interviewer

Your question is Optimize Sequential Block Retrieval. 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.