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
Python Function for a Basic Gen Model
00:00
5 left

Python Function for a Basic Gen Model

HardPython

Problem

JPMorganChase is evaluating lightweight text-generation components for internal AI assistants. Implement a token-level generative model without external machine learning libraries.

Given a training corpus, build an interpolated n-gram model that predicts the next token from the longest available context. Apply additive smoothing so every vocabulary token remains possible, back off to shorter contexts when a context was not observed, and generate a reproducible continuation using a seeded random number generator.

Formal Specification

Implement generate_text(corpus, prompt, order, max_tokens, temperature, seed).

  • corpus and prompt are non-empty whitespace-separated strings.
  • order is the maximum context length.
  • max_tokens is the number of tokens to generate, excluding the prompt.
  • temperature controls randomness. A value of 0 selects the highest-probability token; positive values sample from temperature-adjusted probabilities.
  • seed initializes Python's pseudorandom generator.
  • Return a string containing exactly the generated tokens separated by single spaces. Return an empty string when max_tokens is zero.

Use additive smoothing with a fixed alpha of 0.1. If several tokens have the same highest probability at temperature 0, choose the lexicographically smallest token.

Constraints

  • 1 <= len(corpus.split()) <= 10^4
  • 0 <= max_tokens <= 100
  • 1 <= order <= 5
  • 0 <= temperature <= 2
  • seed is an integer
  • The vocabulary contains at most 2,000 distinct tokens

Function Signature

def generate_text(corpus, prompt, order, max_tokens, temperature, seed):
Interviewer

Your question is Python Function for a Basic Gen Model. 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.