OpenAI software engineer interviews cover algorithms, data structures, system design, and coding problems drawn from real interview rounds.
cd command. Given a current directory path and a relative destination path, return the final absolute...Input: Given inputMemory Allocator Design a memory allocator that manages a contiguous block of memory. Implement malloc and free operations with efficient...
"A.B.C.D", where each octet is an...Input: StringSystem Design Questions - OpenAI A collection of commonly asked system design questions from OpenAI interviews.
Input: Given inputScenario
Part 1 — Retry eligibility and endpoint spacing
id, project, endpoint, event, timestamp, and attempts. timestamp is an integer number of seconds; attempts counts previous failed attempts.response.completed and response.failed. Exclude any other event with reason FILTERED, even if its attempt count would also trigger the dead-letter rule.attempts >= 3 with reason DEAD_LETTER.timestamp when attempts == 0; otherwise it is timestamp + 2^attempts. Thus one previous failure adds 2 seconds and two add 4 seconds.endpoint_gap. Different endpoints may dispatch simultaneously.id. Record one dispatch per eligible record; do not simulate the result of that dispatch.def plan_deliveries(records: list[dict[str, str]]):
...Example 1
{
"records": [
{"id":"a","project":"p","endpoint":"e1","event":"response.completed","timestamp":"0","attempts":"0"},
{"id":"b","project":"p","endpoint":"e1","event":"response.failed","timestamp":"0","attempts":"0"},
{"id":"c","project":"p","endpoint":"e2","event":"response.completed","timestamp":"1","attempts":"2"},
{"id":"d","project":"p","endpoint":"e2","event":"response.completed","timestamp":"0","attempts":"3"},
{"id":"e","project":"p","endpoint":"e3","event":"response.in_progress","timestamp":"0","attempts":"0"}
]
}{
"deliveries": [{"id":"a","at":0},{"id":"b","at":2},{"id":"c","at":5}],
"excluded": [{"id":"d","reason":"DEAD_LETTER"},{"id":"e","reason":"FILTERED"}]
}a dispatches at 0. Although b is ready at 0, its endpoint cannot dispatch again until 0 + 2 = 2. Delivery c is ready at 1 + 2^2 = 5; its endpoint has no previous dispatch. The excluded records consume no scheduling capacity.Part 2 — Caller-defined rolling-window quotas and explanations
quotas: list[dict[str, str]]. Before implementing this extension, discuss how the return type changes to represent multiple blocking rules. Your design must support future quota additions through configuration and new rule kinds through a common rule interface, without rewriting the core scheduling loop.id, scope, key, limit, and window. scope is project or endpoint; key selects exactly one project or endpoint. limit and window are positive integers represented as strings.limit dispatches in the rolling interval (t - window, t], including the proposed dispatch at t. A dispatch exactly window seconds earlier does not count. Each dispatch consumes one unit in every matching quota.limit matching dispatches have already been scheduled, let s be the timestamp of the limit-th most recent matching dispatch. This quota alone requires the new delivery to occur no earlier than s + window. Otherwise it imposes no additional lower bound.{"rule": rule_id, "not_before": integer_time}; sort blockers by rule ID.quotas argument, each delivery additionally contains blockers, including an empty array when no rule delays it. With no quotas argument, retain the Part 1 output. Excluded records are unchanged.Example 2
{
"records": [
{"id":"a","project":"p","endpoint":"e1","event":"response.completed","timestamp":"0","attempts":"0"},
{"id":"b","project":"p","endpoint":"e2","event":"response.completed","timestamp":"0","attempts":"0"},
{"id":"c","project":"p","endpoint":"e1","event":"response.failed","timestamp":"0","attempts":"0"},
{"id":"d","project":"p","endpoint":"e2","event":"response.completed","timestamp":"1","attempts":"1"}
],
"quotas": [
{"id":"project_budget","scope":"project","key":"p","limit":"2","window":"5"}
]
}{
"deliveries": [
{"id":"a","at":0,"blockers":[]},
{"id":"b","at":0,"blockers":[]},
{"id":"c","at":5,"blockers":[{"rule":"endpoint_gap","not_before":2},{"rule":"project_budget","not_before":5}]},
{"id":"d","at":5,"blockers":[{"rule":"project_budget","not_before":5}]}
],
"excluded": []
}a and b use both project slots at time 0. Delivery c is ready at 0, but endpoint spacing requires 2 and the project quota requires 0 + 5 = 5. Delivery d is ready at 1 + 2^1 = 3; its endpoint bound is only 2, so that rule is not a blocker. Both c and d can dispatch at 5, with c first by ID. The two time-0 dispatches are outside (0, 5], and the two time-5 dispatches exactly fill the cap of 2.Example 3
{
"records": [
{"id":"a","project":"p","endpoint":"e1","event":"response.completed","timestamp":"0","attempts":"0"},
{"id":"b","project":"p","endpoint":"e1","event":"response.completed","timestamp":"0","attempts":"0"},
{"id":"c","project":"p","endpoint":"e1","event":"response.failed","timestamp":"0","attempts":"0"},
{"id":"d","project":"p","endpoint":"e1","event":"response.failed","timestamp":"0","attempts":"3"}
],
"quotas": [
{"id":"project_budget","scope":"project","key":"p","limit":"1","window":"5"},
{"id":"endpoint_burst","scope":"endpoint","key":"e1","limit":"2","window":"12"}
]
}{
"deliveries": [
{"id":"a","at":0,"blockers":[]},
{"id":"b","at":5,"blockers":[{"rule":"endpoint_gap","not_before":2},{"rule":"project_budget","not_before":5}]},
{"id":"c","at":12,"blockers":[{"rule":"endpoint_burst","not_before":12},{"rule":"endpoint_gap","not_before":7},{"rule":"project_budget","not_before":10}]}
],
"excluded": [{"id":"d","reason":"DEAD_LETTER"}]
}a dispatches at 0, b must wait until max(0 + 2, 0 + 5) = 5; the endpoint quota still has its second slot available. For c, three rules independently delay its readiness of 0: endpoint spacing requires 5 + 2 = 7, the project quota requires 5 + 5 = 10, and the endpoint quota requires 0 + 12 = 12. At time 12, the endpoint interval (0, 12] contains only b and c, totaling 2, while the project interval (7, 12] contains only c, totaling 1. Record d is excluded before scheduling.Notes
endpoint_gap.ttl). Its validity interval is [timestamp, timestamp + ttl).For get(key, query_time), return the value of the valid write for that key having the greatest write timestamp. If no write is valid at query_time, return None. In particular, an expired newer write does not hide an older write that is still valid.Writes may arrive out of timestamp order, and reads may query any historical time. A write to an existing (key, timestamp) replaces both its value and its ttl. Each read considers only writes processed before that read; replacement also affects subsequent historical reads.Support operations online, without knowing future operations. Target expected O(log n) time per operation, where n is the number of distinct timestamps stored for the relevant key. Scanning expired versions or inserting into a sorted array does not meet this target.Class signatureTemporalSnapshotStore() — initialize an empty store.set(key: str, value: str, timestamp: int, ttl: int) -> None — insert or replace one version.get(key: str, query_time: int) -> str | None — return the newest valid version, or None.{
"operations": [
["set", "routing", "stable", 10, 100],
["set", "routing", "canary", 20, 5],
["get", "routing", 24],
["get", "routing", 25],
["get", "routing", 110]
]
}null for each set:[null, null, "canary", "stable", null]
canary is valid on [20, 25), while stable is valid on [10, 110). At time 25, the canary has expired, so the older stable snapshot is selected. At time 110, both have expired.Example 2:{
"operations": [
["set", "model-config", "newer", 30, 10],
["set", "model-config", "older", 10, 50],
["set", "model-config", "replacement", 30, 3],
["get", "model-config", 32],
["get", "model-config", 33],
["get", "model-config", 9]
]
}[null, null, null, "replacement", "older", null]
set and get calls.0 <= timestamp, query_time <= 10^12.1 <= ttl <= 10^12.def max_non_adjacent_score(scores: List[int]) -> int: [3, 2, 5, 10, 7] 15 3, 10, and 2 to get 3 + 10 + 2 = 15, skipping the adjacent 2 and 5.[1, 2, 3, 1] 4 1 and 3 to get 1 + 3 = 4, avoiding the adjacent scores.0 <= scores.length <= 1000 0 <= scores[i] <= 1000m x n grid filled with non-negative integers, find a path from the top-left corner to the bottom-right corner, which minimizes the sum of the values along the path. You can only move down or right at any point in time.def min_path_sum(grid: List[List[int]]) -> int:[[1,3,1],[1,5,1],[4,2,1]]7[[1,2,3],[4,5,6]]121 <= m, n <= 1000 <= grid[i][j] <= 100n integers and a target integer target, determine if there's a subset of the given integers that adds up to exactly the target value. You should implement a function that efficiently solves this problem using dynamic programming.def subset_sum(nums: List[int], target: int) -> bool:nums = [3, 34, 4, 12, 5, 2], target = 9 True {4, 5} adds up to 9.nums = [1, 2, 3, 7], target = 6 True {1, 2, 3} adds up to 6.1. def multi_source_shortest_path(edges: List[Tuple[int, int]], sources: List[int]) -> Dict[int, int]:edges = [(0, 1), (1, 2), (0, 2), (2, 3)], sources = [0] {0: 0, 1: 1, 2: 1, 3: 2} 0 are as follows: to itself is 0, to 1 is 1, to 2 is 1, and to 3 is 2.Example 2: edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 1)], sources = [1, 3] {0: 2, 1: 0, 2: 1, 3: 0, 4: 1} 1 and 3.Constraints: 1 and 10^5. 1 and 10^5. 0 to V-1. labelers, a list of tasks with required time to complete, and models that need this data, create a function that returns a balanced assignment of tasks to each labeler. Every label should have an equitable workload within a specified limit.Function/class signature:def assign_tasks(labelers: List[str], tasks: List[int], max_load: int) -> Dict[str, List[int]]:labelers = ['Alice', 'Bob', 'Charlie'], tasks = [3, 1, 4, 2, 5], max_load = 5{'Alice': [3, 2], 'Bob': [1, 4], 'Charlie': [5]}max_load of 5.labelers = ['Dave', 'Eve'], tasks = [1, 2, 3], max_load = 3{'Dave': [1, 2], 'Eve': [3]}1 <= len(labelers) <= 101 <= len(tasks) <= 1000 <= tasks[i] <= 10max_load > 0costs where each integer represents the cost to step on that cell, write a function to find the minimum cost to reach the bottom right corner of the grid from the top left corner. You can only move down or right at any point in time.def min_cost_path(costs: List[List[int]]) -> int:[[1, 3, 1], [1, 5, 1], [4, 2, 1]]71 -> 3 -> 1 -> 1 -> 1, total cost = 7.[[10, 2], [1, 1]]310 -> 1 -> 1, total cost = 3.nums, return the length of the longest increasing subsequence. A subsequence is formed by removing elements from the array without changing the order of the remaining elements. This problem is essential for analyzing sequences in large datasets relevant to OpenAI's products. def length_of_LIS(nums: List[int]) -> int: nums = [10,9,2,5,3,7,101,18] 4 [2,3,7,101], therefore the length is 4. nums = [0,1,0,3,2,3] 4 [0,1,2,3], therefore the length is 4. 1 <= nums.length <= 2500 -10^4 <= nums[i] <= 10^4 k.Function Signature
def max_sum_subarray_of_k(arr: List[int], k: int) -> int: Returns the maximum sum of the subarray of size k.Example 1:
arr = [1, 2, 3, 4, 5], k = 312[3, 4, 5] with a sum of 12.Example 2:
arr = [2, 3, 4, 1, 5], k = 27[4, 1].Constraints:
0 < len(arr) <= 10001 <= k <= len(arr)Function Signature
def max_non_adjacent_sum(tokens: List[int]) -> int: Returns the maximum sum of non-adjacent integers.Example 1:
[3, 2, 5, 10, 7]15Example 2:
[9, 1, 2, 8, 3]17Constraints:
0 < len(tokens) <= 10000 <= tokens[i] <= 1000Class Definition
MedianFinder should implement the following methods:addNum(num: int) -> None: Adds a number to the data structure.findMedian() -> float: Returns the median of all added numbers.Example 1:
medianFinder = MedianFinder(); medianFinder.addNum(1); medianFinder.addNum(2); medianFinder.findMedian()1.5Example 2:
medianFinder.addNum(3); medianFinder.findMedian()2Constraints:
-10^5 <= num <= 10^51,000,000 calls to addNum and findMedian.Sign up for free to access walkthroughs, AI-generated questions, and more.
Get Started Free