FastPrepClone an Undirected Graph State
Problems/Clone an Undirected Graph State

Clone an Undirected Graph State

Apple logoApple● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Apple new grad online assessment. Deep copying a connected undirected graph is about 80% the same as the report. We added two things to catch bugs from shared state: you may edit only the clone's data and edges, and the answer includes paired snapshots. We also chose what makes a graph valid and the input limits.

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 node u in the clone to x. Nothing else changes.
  • [1,u,v] flips the edge between two different nodes u and v in 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. Here replacementData is the new data x in 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 u lists v, then v also lists u.
  • 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

Interview reports that mention this question

  • Apple India SWE New Grad Interviews: 5 Experiences

Apple interview experiences

  • Apple SWE Phone Screen: Subarray Products: 1 Experience
  • Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
  • Apple AI Coding and Related Interviews: 17 Experiences
  • Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
  • Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)
All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public int[][][] cloneGraphState(int[] values, int[][] neighbors, int[][] edits) {
        // Write your solution here.
    }

}
Checking account…