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
Custom Attention Mechanism
00:00
5 left

Custom Attention Mechanism

HardPython

Problem

CoreWeave inference workloads often need customized attention behavior that cannot rely on a framework primitive. Implement single-head scaled dot-product attention using only Python lists and the standard library.

Given query matrix Q, key matrix K, value matrix V, an optional boolean mask, and a causal flag, compute:

Attention(Q, K, V) = softmax((Q × Kᵀ) / sqrt(d)) × V

where d is the key dimension. A mask value of true means the key may be attended to, while false excludes it. If causal is true, position i may attend only to key positions j <= i. Apply both restrictions when both are provided. Every query row has at least one permitted key.

Formal Specification

Implement attention(q, k, v, mask, causal). q is an n x d list of numbers, k is an m x d list, and v is an m x dv list. mask is either None or an n x m boolean matrix. causal is a boolean. Return an n x dv list of floating-point numbers. Do not use NumPy or machine-learning libraries. Use a numerically stable softmax by subtracting the largest permitted score before exponentiation.

Constraints

  • 1 <= n, m, d, dv <= 128
  • q has shape n x d, k has shape m x d, and v has shape m x dv
  • mask is None or has shape n x m
  • Every query row has at least one permitted key
  • All input values are finite and have absolute value at most 100

Function Signature

def attention(q, k, v, mask, causal):
Interviewer

Your question is Custom Attention Mechanism. 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.