Array Values at Saved Snapshot Versions

Quick Overview

Array Values at Saved Snapshot Versions. Implement the native SnapshotArray class. Construction creates length zeros.

Array Values at Saved Snapshot Versions

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Implement the native SnapshotArray class. Construction creates length zeros. set(index,val) changes the current value. snap() saves the current array and returns successive IDs 0,1,... . get(index,snap_id) returns that index's value in the named completed snapshot. Later writes must not change earlier snapshots. ```cpp class SnapshotArray { public: SnapshotArray(int length); void set(int index, int val); int snap(); int get(int index, int snap_id); }; ``` ### Examples **Example 1:** ```text Input: construct SnapshotArray(2); set(1,4); snap(); set(1,9); snap(); get(1,0); get(1,1); get(0,1) Output: snap results0,1; get results4,9,0 Explanation: The untouched index stays0, and the first snapshot retains4. ``` **Example 2:** ```text Input: construct SnapshotArray(1); set(0,2); set(0,7); snap(); get(0,0) Output: snap result0; get result7 Explanation: The latest write before the snapshot is saved. ``` ### Constraints - The constructor length is between 1 and 50000. - Indices satisfy `0 <= index < length`. - Set values are in `[0,1000000000]`. - A queried snapshot ID is nonnegative and smaller than the number of completed snap calls. - At most 50000 calls in total are made to set, snap and get. ### Contract considerations Keep the constructor and set/snap/get methods. Call sequences in examples illustrate the native object protocol, not a replacement batch API. ```hint Read the boundary condition If set is called twice before snap, which value belongs to that snapshot, and can a later set change it? ``` ### Boundary reminders Keep the constructor and set/snap/get methods. Call sequences in examples illustrate the native object protocol, not a replacement batch API.

Overview: Array Values at Saved Snapshot Versions. Implement the native SnapshotArray class. Construction creates length zeros.

Apple logo
Apple
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement the native SnapshotArray class. Construction creates length zeros. set(index,val) changes the current value. snap() saves the current array and returns successive IDs 0,1,... . get(index,snap_id) returns that index's value in the named completed snapshot. Later writes must not change earlier snapshots.

class SnapshotArray {
public:
    SnapshotArray(int length);
    void set(int index, int val);
    int snap();
    int get(int index, int snap_id);
};

Examples

Example 1:

Input: construct SnapshotArray(2); set(1,4); snap(); set(1,9); snap(); get(1,0); get(1,1); get(0,1)
Output: snap results0,1; get results4,9,0
Explanation: The untouched index stays0, and the first snapshot retains4.

Example 2:

Input: construct SnapshotArray(1); set(0,2); set(0,7); snap(); get(0,0)
Output: snap result0; get result7
Explanation: The latest write before the snapshot is saved.

Constraints

  • The constructor length is between 1 and 50000.
  • Indices satisfy 0 <= index < length .
  • Set values are in [0,1000000000] .
  • A queried snapshot ID is nonnegative and smaller than the number of completed snap calls.
  • At most 50000 calls in total are made to set, snap and get.

Contract considerations

Keep the constructor and set/snap/get methods. Call sequences in examples illustrate the native object protocol, not a replacement batch API.

Boundary reminders

Keep the constructor and set/snap/get methods. Call sequences in examples illustrate the native object protocol, not a replacement batch API.