Coding interview questions for data scientists and ML engineers

Coding interviews give you a problem to solve live in an editor while you talk. Interviewers watch how you clarify the problem, reach a working approach, reason about complexity and test your own code.

Data and AI interviews test SQL and coding, statistics and machine learning concepts, and ML system design. Expect follow-ups on how you would measure a model, what breaks in production, and why you chose one approach over another.

8 questions below, each one a practice interview: the AI interviewer asks it out loud, follows up on your answer, and sends you a scored feedback report.

Practise free with the AI interviewer

What the interviewer scores

  • Problem Solving

    Clarifies the problem, explores examples, reaches a working approach and reasons about its complexity before coding.

  • Correctness

    The final code solves the problem; runs pass or failures are understood and fixed.

  • Code Quality

    Readable structure, clear names, idiomatic use of the language, no needless complexity.

  • Testing & Edge Cases

    Names edge cases unprompted, tests or traces the code with examples, and fixes what testing reveals.

  • Communication

    Explains their thinking out loud, answers the question asked, checks assumptions and responds to hints and follow-ups.

Questions

Two Sum

Coding · Easy · 20 min · Free to practise

Given an array of integers nums and an integer target, return the indices of the two numbers that add up to target.

You may assume exactly one valid answer exists, and you may not use the same element twice.

Example

Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]

Discuss your approach out loud before coding. What is the brute-force complexity? Can you do better?

Practise this question with the AI interviewer

Sessionize Clickstream Events

Coding · Medium · 35 min · Free to practise

A product analytics team at an online learning site wants to turn raw clickstream events into sessions.

Write sessionize(events, gapSeconds). Each event is (userId, timestampSeconds); the list is not sorted and may mix users. For each user, events belong to the same session while the time since that user's previous event is at most gapSeconds; a longer gap starts a new session. Return every session as (userId, startTimestamp, endTimestamp, eventCount), sorted by userId, then by startTimestamp.

Example (gapSeconds = 1800)

  • Events: ("u1", 0), ("u1", 600), ("u2", 100), ("u1", 3000), ("u1", 3100), ("u2", 2000)
  • Output: ("u1", 0, 600, 2), ("u1", 3000, 3100, 2), ("u2", 100, 100, 1), ("u2", 2000, 2000, 1)

A gap of exactly gapSeconds stays in the same session. A user with a single event has one session of zero length.

Constraints: up to 1,000,000 events and 100,000 users; timestamps are non-negative integers. Use any language.

Before coding, say how you will group and order the events and what the time and memory cost is. Then discuss how the approach would change if events arrived as a live stream, slightly out of order.

Practise this question with the AI interviewer

Build a Weekly Retention Cohort Table

Coding · Medium · 35 min

A subscription app's growth team wants a weekly retention table built straight from raw data, without an analytics tool.

Write retentionCohorts(signups, activity, asOfDay, numWeeks).

  • signups: list of (userId, signupDay). Days are integers counted from launch (day 0).
  • activity: list of (userId, day), one row per active user-day, in any order. It may contain users with no signup row and days before a user's signup; ignore both.
  • A user's cohort is signupDay // 7. Their week k covers days signupDay + 7k through signupDay + 7k + 6, relative to their own signup.
  • For every cohort that has at least one signup, return (cohortWeek, cohortSize, counts) where counts[k] is the number of cohort members active at least once in their week k, for k from 0 to numWeeks - 1. If any member's week-k window ends after asOfDay, that week is not fully observed: report -1 for it.

Example (asOfDay = 20, numWeeks = 3)

  • Signups: (a, 0), (b, 3), (c, 8)
  • Activity: (a, 2), (a, 9), (b, 3), (b, 12), (c, 8), (c, 20), (x, 5), (a, 15)
  • Output: (0, 2, [2, 2, -1]), (1, 1, [1, -1, -1])

Up to 500,000 signups and 20,000,000 activity rows. Explain your approach and its cost before coding, and say why the -1 rule matters to whoever reads the table.

Practise this question with the AI interviewer

Compute ROC AUC from Scratch

Coding · Medium · 35 min

A team at a lending startup cannot install libraries in its scoring sandbox and needs to compute the ROC AUC of a classifier itself.

Write rocAuc(scores, labels). scores are model outputs (any real numbers, higher means more likely positive) and labels are 0 or 1, the same length. Return the probability that a randomly chosen positive example is scored higher than a randomly chosen negative one, where a tie in score counts as half. If there are no positives or no negatives, the AUC is undefined: signal that clearly instead of returning a number.

Example

  • scores = [0.9, 0.8, 0.8, 0.3, 0.1], labels = [1, 0, 1, 0, 0]
  • Positive-negative pairs: 6. The 0.9 positive beats all three negatives (3); the 0.8 positive ties one negative and beats two (2.5). AUC = 5.5 / 6, about 0.9167.

Constraints: up to 2,000,000 examples, many tied scores. Comparing every positive with every negative is too slow at this size, so aim for O(n log n).

Start from the pairwise definition, then explain how sorting gets you there and how you handle ties. Test with your own edge cases.

Practise this question with the AI interviewer

Implement K-Means Clustering

Coding · Medium · 40 min

A retail analytics team wants to cluster stores by two numbers (say, average basket size and weekly visits) and needs k-means written by hand for an environment with no numeric libraries.

Write kMeans(points, initialCentroids, maxIterations). Points and centroids are lists of 2D coordinates. Repeat until no point changes cluster or maxIterations is reached:

  1. Assign each point to its nearest centroid by Euclidean distance; on a tie, choose the lower centroid index.
  2. Move each centroid to the mean of its assigned points. A centroid with no points keeps its previous position.

Return the final centroids and each point's cluster index.

Example

  • Points: (1,1), (1,2), (2,1), (8,8), (9,8), (8,9); initial centroids (1,1) and (1,2); maxIterations = 10.
  • After the first pass the second centroid moves toward the far group; the run converges to centroids about (1.33, 1.33) and (8.33, 8.33), with clusters [0, 0, 0, 1, 1, 1].

Up to 100,000 points and 50 clusters. Explain the cost per iteration before coding. Afterward, be ready to discuss how you would choose initial centroids and k, why the features' scales matter, and what k-means assumes about cluster shape.

Practise this question with the AI interviewer

SQL: Subscription Churn by Plan

Coding · Medium · 35 min · Free to practise

You are a data analyst at Platewise, a meal-planning app sold as a monthly subscription on three plans. The growth lead has heard that "Premium is bleeding customers" and wants numbers before a pricing meeting.

The tables are on the left of the editor: plans, customers and subscriptions. A customer can hold more than one subscription over time; ended_on is empty while a subscription is still running.

Your interviewer will ask for a few queries in turn, starting with how many subscribers each plan has at the end of June 2026 and building toward June's churn. Write each query in the editor and run it; the database is rebuilt before every run.

Talk through as you go

  • How you define "active" and "churned", and why that definition matters.
  • Edge cases in the data: dates on the boundary, customers who switch plans, people who join and leave in the same month.
  • Whether the result answers the question the growth lead actually has.
Practise this question with the AI interviewer

Resolve Duplicate Customer Records

Coding · Hard · 45 min · Free to practise

A home-services marketplace merged two customer databases and now has duplicate people. You are asked to group records that refer to the same customer.

Each record has id, name, email, phone and zip; any of name, email or phone may be empty. Normalize before comparing:

  • email: trim and lowercase;
  • phone: keep digits only, then the last 10 digits;
  • name: lowercase, remove punctuation, collapse spaces.

Two records match when they share a non-empty email, or share a non-empty phone, or have the same zip and names within an edit distance of 1 (one insertion, deletion or substitution). Matching is transitive: if A matches B and B matches C, all three are one customer. Return the groups as sorted lists of ids, ordered by their smallest id.

Example

  • 1: Ana Souza, ana@ex.com, (555) 010-2000, 30301
  • 2: Anna Souza, (no email), (no phone), 30301
  • 3: J. Okafor, jo@ex.com, 555-010-3000, 60601
  • 4: Jide Okafor, " JO@ex.com", (no phone), 10001
  • 5: Mei Lin, (no email), 5550102000, 94105
  • 6: Mei Lin, (no email), (no phone), 73301
  • Output: [1, 2, 5], [3, 4], [6]

Up to 200,000 records; one zip can hold 5,000 of them. Comparing every pair is too slow: explain how you avoid it before you code.

Practise this question with the AI interviewer

SQL: Seller Order Streaks

Coding · Hard · 40 min

You are an analyst at Tallowmade, a marketplace for handmade goods. The seller-success team wants to reward consistency: sellers who make sales day after day, not only big one-off weeks.

The tables are on the left of the editor: sellers and orders. Refunded orders stay in orders with status = 'refunded', and a seller can have several orders on the same day.

Your interviewer will ask you to rank sellers by revenue, then find each seller's longest run of consecutive days with at least one sale, with the dates it started and ended. The database is SQLite 3.27: window functions such as ROW_NUMBER() and RANK() work.

Talk through as you go

  • What counts as revenue and what counts as a sale day.
  • How you turn individual days into streaks, and how you test that your query is right.
  • Ties, and what the team should do with the result.
Practise this question with the AI interviewer