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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Top-k and Top-p Sampling
00:00
5 left

Top-k and Top-p Sampling

HardPython

Problem

Write a function to simulate top-k or top-p (nucleus) sampling during text generation at Mistral AI.

Implement sample_token(logits, top_k=None, top_p=None, seed=0), returning one sampled token index. logits is a non-empty list of floats, exactly one of top_k or top_p is provided, and seed must make sampling reproducible. Apply softmax only after filtering, then sample proportionally to the remaining probabilities. For top-p, sort tokens by descending logit and retain tokens through the first cumulative probability reaching top_p.

Constraints

  • 1 <= len(logits) <= 10000
  • All logits are finite floating-point values
  • Exactly one of top_k and top_p is provided
  • 1 <= top_k <= len(logits)
  • 0 < top_p <= 1
  • Return a token index from 0 through len(logits) - 1
  • The same inputs and seed must produce the same result

Function Signature

def sample_token(logits, top_k=None, top_p=None, seed=0):
Interviewer

Your question is Top-k and Top-p Sampling. 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.