Clone an Undirected Graph State
Updated
Problem statement
You are given a connected undirected graph. Make a full, separate copy of it, called the clone. Then apply a list of edits to the clone only. Return the final state of both graphs. This shows that editing the clone never changes the original.
Nodes are numbered 1 through n. Node i holds the data values[i-1], and neighbors[i-1] is its sorted list of neighbors. At the start, no node has an edge to itself, and no two edges join the same pair of nodes. Every edge appears in the neighbor lists of both of its nodes.
There are two kinds of edits:
[0,u,x]sets the data of nodeuin the clone tox. Nothing else changes.[1,u,v]flips the edge between two different nodesuandvin the clone. If the edge is missing, add it. If it is there, remove it.
Return [originalRows,clonedRows]. In each of these, row i-1 is [nodeData,neighbor1,neighbor2,...]: the node's data followed by its neighbors in increasing order. Edits may split the clone into separate pieces. That is fine, and you do not need to do any extra work on the graph because of it. The original rows must stay unchanged.
Function
cloneGraphState(values: int[], neighbors: int[][], edits: int[][]) → int[][][]Examples
Example 1
values = [7,8]neighbors = [[2],[1]]edits = [[0,1,9],[1,1,2]]return = [[[7,2],[8,1]],[[9],[8]]]The first edit replaces the clone's data for the first node. The second edit removes the only edge in the clone, since that edge was there. The original keeps both of its old values and its edge, which still appears in both of its rows. So the clone's rows hold data only, with no neighbors.
Example 2
values = [4]neighbors = [[]]edits = []return = [[[4]],[[4]]]The graph has a single node with no neighbors, and there are no edits. The clone is still made, and it matches the original, so both snapshots are the same.
Constraints
1 <= values.length = neighbors.length <= 2000.-10^9 <= values[i], replacementData <= 10^9. HerereplacementDatais the new dataxin an edit that changes a node's data.- At the start, every neighbor list is strictly increasing and holds only valid node IDs. If node
ulistsv, thenvalso listsu. 0 <= edits.length <= 2000. Every edit is valid and has one of the two forms described above.
More Apple interview questions
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge Two Strings by Maximum Boundary OverlapOA · Seen Sep 2026
- Collect Letters Along a Unique Grid PathOA · Seen Aug 2025
- Maximum Weight in Two BagsOA · Seen Aug 2025
- Ignore SectionsOA · Seen Mar 2024
- Gas Station Circular TourOA · Seen Nov 2021
- House Robber IIOA · Seen Nov 2021
- Longest Increasing Subsequence LengthOA · Seen Feb 2021
- Maximum Later DifferenceOA · Seen Feb 2021
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Corporation Reporting LinesMicrosoft · PHONE SCREENMicrosoft interview questions
- Agent Task Dependency TrackerPerplexity AI · PHONE SCREENPerplexity AI interview questions
- Propagate Spreadsheet Cell UpdatesOpenAI · PHONE SCREENOpenAI interview questions
- Spreadsheet Formula DependenciesCitadel · PHONE SCREENCitadel interview questions
- Spreadsheet With Dependent CellsCharacter.AI · PHONE SCREEN
- Evaluate Live Spreadsheet ExpressionsStrala · PHONE SCREEN
- Spreadsheet Formula DependenciesAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- LRU CacheTypeSafe AI · OA
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions
- Corporate Card Expense RulesRippling · OA · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions