← Questions

C++ performance round with provided threading code

0
OpenAICodingLast asked Sep 2025

A coding interview question asked in OpenAI's technical screen (coding), last asked Sep 2025. It tests c++, performance and concurrency.

A 60-minute C++ round on making code fast. You get a working program whose threading pieces (threads, work queues, locks) are already written. You do not write threading code; you find out why the program is slow, fix it, and explain each choice. A smaller part is about using the provided concurrency well.

Part 1: make the program fast

Profile the program, find the bottlenecks and fix them at the code level. Expect to be asked what you measure and how you show the change helped without breaking correctness under concurrency.

Part 2: how many workers?

A smaller exercise on the same theme. tasks[i] is the cost of task i, in execution order. To keep cache locality, each active worker gets one non-empty contiguous block of tasks, and the blocks cover the array in order. With m active workers the runtime is max(block_sum) + overhead * m, where overhead is the per-worker cost of scheduling and synchronization.

Unless you agree on a different interface with the interviewer, assume:

  • min_runtime(tasks: list[int], k: int, overhead: int) -> int returns the smallest runtime using at most k workers, or 0 for no tasks.
min_runtime([10, 3, 3], 2, 8)    ->  24    # 1 worker: 16 + 8; 2 workers: 10 + 16 = 26
min_runtime([5, 1, 2], 10, 2)    ->  9     # [5] [1, 2]: 5 + 2*2; at most 3 workers fit
min_runtime([4, 4, 4, 4], 4, 1)  ->  8     # 4 workers: 4 + 4; 2 workers: 8 + 2 = 10

Constraints

  • 0 <= len(tasks) <= 300, 0 <= tasks[i] <= 10^6, 1 <= k <= 50, 0 <= overhead <= 10^6.

Clarifying questions

  • What is the target: throughput, tail latency, or both? What hardware and compiler flags?
  • Can I change data structures and interfaces, or only function bodies?
  • Is there a benchmark and a correctness test I can run before and after?
  • In Part 2, must every one of the m workers get at least one task?

What a strong answer covers

  • Measuring before changing anything: a profiler (perf, VTune, a flame graph), cache-miss and branch-miss counters, allocation counts.
  • The usual C++ costs: copies where a move or a reference would do, allocation in hot loops (reserve, reuse buffers), node-based containers where a flat vector keeps data in cache, needless virtual calls, false sharing between threads writing neighbouring data.
  • Less lock contention with the given tools: shorter critical sections, batching work per lock, per-thread buffers merged at the end.
  • Part 2: prefix sums plus a DP over workers and split point, O(k n^2), or a binary search on the largest block for each m; trying every m up to min(k, n).

Follow-ups

  • After your changes a test fails once in 200 runs. How do you find the race?
  • When is a lock-free structure worth it here, and what does it cost?
  • In Part 2, tasks can also be reordered. How does that change the problem?

Where it's asked

  • OpenAI · Technical screen: coding · Last asked Sep 202560 min · One problem in three to five growing parts filling the hour, usually in CoderPad; ML roles get a Colab notebook with autocomplete off · No AI assistants or autocomplete; documentation is fine

What it tests

c++ · performance · concurrency

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding