Google software engineer interviews cover algorithms, data structures, system design, and coding problems drawn from real interview rounds.
Question Given a string where letters are sorted in alphabetical order, identify all letters that appear more than twice and record their first and...
Input: ArrayQuestion You are tracking GPS location errors by comparing measured GPS locations against a set of "golden" (reference) locations. Each location...
Input: ListQuestion Design a data structure to maintain a dynamic set of points on a 2D coordinate plane. Support operations to insert points, remove points,...
Input: ListQuestion Given a singly linked list, reverse the second half of the list and then interleave the nodes from the first half and the reversed second...
Input: Linked listQuestion You are given a range [A, B] and a sequence of painting operations. For each operation [L, R], calculate the total length of unpainted...
Input: Array of intervalsQuestion You are given a set of test cases and a black-box function runTests() that accepts a subset of these test cases and returns whether...
Question You are given an M x N garden grid and a list of crops, each requiring a specific number of plots. The total number of plots required by...
Input: 2D gridQuestion You are given an array A of integers. A triplet is a sequence of three consecutive elements. A triplet is called zero-sum if the...
Question A player is playing a game in which coins are placed on and removed from a table. The game consists of multiple rounds. At the beginning...
Input: Stringtext and a dictionary array where each element is in the format "<key>:<id>". Here key is a token string and id...Input: ArrayStreamBuffer class that buffers a stream of integer latency samples in FIFO order and supports O(1) access to both the minimum and maximum...Input: Integer(s)Question Design a PathRouter class that maps URL-like path patterns to handler names. Patterns may contain wildcard segments (*) that match any...
Question Given a string, build a Frequency Merge Tree as follows: 1. Count the frequency of each character in the string. 2. Create a leaf node...
Input: String+ and -, and parentheses ( and ), simplify...Input: Stringm x n binary grid where each cell is either '1' (land) or '0' (water). A group of connected land cells (connected horizontally...Input: 2D gridid, project, start, end, slots, and savings. Times are integer minutes relative to a common epoch; execution occupies [start, end).BASE_MIN_SAVINGS rejects savings below 5.00; SLOT_CAP rejects requests above 100 slots. Report every violated rule.plan_queries(jobs: list[dict[str, str]]) -> candidate_defined_typeExample 1:{
"jobs": [
{"id":"a","project":"analytics","start":"0","end":"30","slots":"80","savings":"8.00"},
{"id":"b","project":"analytics","start":"20","end":"50","slots":"100","savings":"12.00"},
{"id":"c","project":"reporting","start":"30","end":"60","slots":"60","savings":"9.00"},
{"id":"d","project":"reporting","start":"60","end":"70","slots":"20","savings":"4.00"},
{"id":"e","project":"analytics","start":"60","end":"80","slots":"120","savings":"15.00"}
]
}{
"selected_ids": ["a", "c"],
"total_savings": "17.00",
"rejected": [
{"id":"d","rules":["BASE_MIN_SAVINGS"]},
{"id":"e","rules":["SLOT_CAP"]}
]
}a and c touch without overlapping and save 8.00 + 9.00 = 17.00, exceeding b alone at 12.00. Query d fails the 5.00 savings floor; e exceeds the 100-slot cap.Part 2 — Project policies and a launch budgetproject, min_savings, max_duration, and cooldown. The planner also receives an integer max_jobs, limiting the number of selected queries.PROJECT_MIN_SAVINGS rejects savings below min_savings, and PROJECT_MAX_DURATION rejects execution duration end - start above max_duration.end + cooldown, even if the next query belongs to another project. Its effective occupied interval is [start, end + cooldown). The duration rule still measures only end - start.max_jobs queries. A limit of zero is valid and produces an empty schedule, but eligibility checks must still run.reservations, in execution order, containing each selected query's id and string-valued available_at, equal to end + cooldown.plan_with_policies(jobs: list[dict[str, str]], policies: list[dict[str, str]], max_jobs: int) -> candidate_defined_typeExample 2:{
"jobs": [
{"id":"a","project":"alpha","start":"0","end":"20","slots":"80","savings":"8.00"},
{"id":"b","project":"beta","start":"20","end":"40","slots":"90","savings":"11.00"},
{"id":"c","project":"alpha","start":"30","end":"50","slots":"70","savings":"10.00"},
{"id":"d","project":"beta","start":"50","end":"70","slots":"100","savings":"13.00"},
{"id":"e","project":"beta","start":"70","end":"90","slots":"60","savings":"9.00"}
],
"policies": [
{"project":"alpha","min_savings":"10.00","max_duration":"30","cooldown":"10"},
{"project":"beta","min_savings":"5.00","max_duration":"25","cooldown":"0"}
],
"max_jobs": 2
}{
"selected_ids": ["b", "d"],
"total_savings": "24.00",
"rejected": [
{"id":"a","rules":["PROJECT_MIN_SAVINGS"]}
],
"reservations": [
{"id":"b","available_at":"40"},
{"id":"d","available_at":"70"}
]
}a passes the base floor but fails alpha's 10.00 floor. All execution durations are 20, within the respective limits. Query c would occupy [30, 60), conflicting with both b and d. The cap prevents selecting b, d, and e together, which would save 33.00. The best allowed pair is b + d = 11.00 + 13.00 = 24.00, versus d + e = 22.00, b + e = 20.00, and c + e = 19.00.Example 3:{
"jobs": [
{"id":"u","project":"alpha","start":"0","end":"20","slots":"101","savings":"4.00"},
{"id":"v","project":"alpha","start":"0","end":"10","slots":"100","savings":"10.00"},
{"id":"w","project":"beta","start":"10","end":"20","slots":"100","savings":"10.00"},
{"id":"x","project":"beta","start":"15","end":"25","slots":"100","savings":"10.00"},
{"id":"y","project":"beta","start":"25","end":"35","slots":"100","savings":"10.00"}
],
"policies": [
{"project":"alpha","min_savings":"10.00","max_duration":"15","cooldown":"5"}
],
"max_jobs": 2
}{
"selected_ids": ["v", "x"],
"total_savings": "20.00",
"rejected": [
{"id":"u","rules":["BASE_MIN_SAVINGS","PROJECT_MAX_DURATION","PROJECT_MIN_SAVINGS","SLOT_CAP"]}
],
"reservations": [
{"id":"v","available_at":"15"},
{"id":"x","available_at":"25"}
]
}u violates four rules: 4.00 < 5.00, duration 20 > 15, 4.00 < 10.00, and 101 > 100 slots. Query v releases the reservation at 10 + 5 = 15, so it conflicts with w but can precede x. Several two-query schedules save 10.00 + 10.00 = 20.00; ["v", "x"] is lexicographically smallest. The launch cap prevents the otherwise compatible three-query schedule ["v", "x", "y"].Notes:2,000 jobs, IDs are unique nonempty ASCII strings of length at most 64, and each project has at most one policy; in Part 2, 0 <= max_jobs <= 30.0 <= start < end <= 10^9, slots are positive integers, savings are nonnegative decimal strings with exactly two fractional digits, and monetary values fit in signed 64-bit cents when summed. Policies have min_savings >= 5.00, positive max_duration, and nonnegative cooldown, each time quantity at most 10^9.(origin, destination, departure, arrival), where airport names are strings and times are integer minutes measured from a common epoch. Each flight can be used only at its listed departure time.A traveler is at airport source at time ready_time and wants to reach target as early as possible. The following rules apply:source at a time in the inclusive interval [ready_time, ready_time + max_layover]. The minimum connection time does not apply before the first flight.t, the next flight must depart from that airport in the inclusive interval [t + min_connection, t + max_layover].target among all valid itineraries, or -1 if none exists. If source == target, return ready_time without taking a flight.A single earliest-arrival label per airport is not necessarily sufficient: arriving too early can make a later departure violate the maximum layover.Function signatureearliest_arrival(flights: list[tuple[str, str, int, int]], source: str, target: str, ready_time: int, min_connection: int, max_layover: int) -> int{
"flights": [["S", "A", 0, 2], ["S", "A", 1, 6], ["A", "T", 8, 10]],
"source": "S",
"target": "T",
"ready_time": 0,
"min_connection": 1,
"max_layover": 3
}10
S are initially available because their departures lie in [0, 3]. Arriving at A at time 2 permits departures only in [3, 5], so that arrival cannot connect. Arriving at time 6 permits departures in [7, 9], allowing the flight departing at 8 and arriving at T at 10.Example 2:{
"flights": [["S", "A", 0, 2], ["A", "T", 6, 8]],
"source": "S",
"target": "T",
"ready_time": 0,
"min_connection": 1,
"max_layover": 3
}-1
A at time 2, but the permitted connection window is [3, 5]. The departure at 6 is too late.Constraints:0 <= len(flights) <= 200000.200000 distinct airports occur across the flights, source, and target.0 <= departure < arrival <= 10^12.0 <= ready_time <= 10^12.0 <= min_connection <= max_layover <= 10^12.2 * 10^12.O(m log(m + 1)) time and O(m + 1) auxiliary space, where m is the number of flights; do not explicitly construct all possible flight-to-flight connections.start and end. Your task is to determine if there exists a path from start to end using DFS. The graph is undirected and can contain cycles. Return true if a path exists; otherwise, return false. def has_path(graph: List[List[int]], start: int, end: int) -> bool:graph = [[1, 2], [2, 3], [3], []], start = 0, end = 3 True 0 -> 1 -> 2 -> 3. graph = [[1], [2], [3], []], start = 0, end = 3 True 1 <= len(graph) <= 1000N neighbors.def dijkstra(graph: Dict[str, List[Tuple[str, int]]], start: str, target: str) -> Tuple[int, List[str]]:graph = { 'A': [('B', 1), ('C', 4)], 'B': [('C', 2), ('D', 5)], 'C': [('D', 1)], 'D': [] }, start = 'A', target = 'D'(4, ['A', 'B', 'C', 'D'])graph = { 'A': [('B', 2)], 'B': [('C', 2), ('D', 1)], 'C': [], 'D': [] }, start = 'A', target = 'C'(4, ['A', 'B', 'C'])def shortest_path(graph: Dict[int, List[Tuple[int, int]]], src: int, target: int) -> Tuple[int, List[int]]:graph = {0: [(1, 5), (2, 10)], 1: [(3, 3)], 2: [(3, 1)], 3: []}, src = 0, target = 3(8, [0, 1, 3])graph = {0: [(1, 2), (2, 4)], 1: [(2, 1), (3, 7)], 2: [(3, 3)], 3: []}, src = 0, target = 3(6, [0, 1, 2, 3])m x n, where each cell represents either a road (0) or an obstacle (1). You need to find the shortest path from the top-left corner (0,0) to the bottom-right corner (m-1,n-1). Return the length of the shortest path, or -1 if there is no path available. def shortestPath(grid: List[List[int]]) -> int: [[0,0,0],[0,1,0],[0,0,0]] 4 [[0,1,0],[1,1,0],[0,0,0]] -1 1 <= m, n <= 100 has_cycle(graph: List[List[int]]) -> bool that returns True if the given graph contains a cycle, and False otherwise.Function/class signature: def has_cycle(graph: List[List[int]]) -> bool: graph = [[1], [2], [0,3], [3]] True graph = [[1, 2], [2], [], [0]] False 1 <= len(graph) <= 10^4 1 <= len(graph[i]) <= 10^4 0 and len(graph) - 1 inclusive.-1. The graph's nodes are represented as integers from 0 to N-1 and edges are represented by pairs of integers (a, b) denoting an edge from a to b.def shortest_path_dag(edges: List[Tuple[int, int]], start: int, target: int, N: int) -> int:edges = [(0, 1), (1, 2), (2, 3)], start = 0, target = 3 3 0 -> 1 -> 2 -> 3, which has a length of 3. edges = [(0, 1), (0, 2), (2, 3)], start = 1, target = 3 -1 1 <= N <= 1000 0 <= edges.length <= 10000 0 <= start, target < N def shortest_path(edges: List[Tuple[int, int, int]], start: int, destination: int) -> Optional[int]: edges = [(0, 1, 2), (1, 2, 3), (0, 2, 7), (2, 3, 1)], start = 0, destination = 3 6 0 -> 1 -> 2 -> 3, which costs 2 + 3 + 1 = 6. edges = [(0, 1, 2), (1, 2, 3), (0, 2, float('inf')), (2, 3, 1)], start = 0, destination = 3 6 0 -> 1 -> 2 -> 3 is still valid and optimal. 1 <= len(edges) <= 10^4 0 <= start < max_node 0 <= destination < max_node float('inf') to denote closed roads.1s in a binary matrix.m x n binary matrix filled with 0s and 1s, your task is to return the area of the largest rectangle containing only 1s. You must implement maximalRectangle(matrix: List[List[int]]) -> int.Example 1:1s in the matrix.Constraints:m == matrix.lengthn == matrix[i].lengthO(m*n) time.findShortestPath(start: Point, end: Point) -> List[Point], which returns the shortest path as a list of Points from the start to the end. Assumptions include that the environment is represented as a 2D grid where passable and non-passable terrains are indicated.Point: A representation of a coordinate with x and y attributes.N x N with N ≤ 1000.0 and N-1.Sign up for free to access walkthroughs, AI-generated questions, and more.
Get Started Free