Pergunta de entrevista da empresa Google

Create the class SnapshottableArray, which is a normal array except it has the ability to take a snapshot in time of the current values of the array, giving a handle/id. It has the class signature below. Focus on reducing space at the cost of time complexity. Fill in the function bodies to complete the class. (I wrote the class with types to make it clear what everything does, but the code should be in JS.) class SnapshottableArray{ SnapshottableArray(int size); // create new snapshottable array with size void set(int index, int value); // set's array at index to value int get(int index); // return the array at index's value int takeSnapshot(); // take a snapshot and return the handle/id of it int getSnapshotValue(int id, int index); // return the array at index's value for the given snapshot, represented by id }

Resposta da entrevista

Sigiloso

31 de jan. de 2019

The idea was the save the changes, I called them deltas, between snapshots which track which the previous value of the element at the index was in the previous array. Only need to take deltas of elements that change between snapshots, which saves space. To recover a previous snapshot's value, walk back over the previous snapshots' deltas until you get to the desired snapshot. While you walk back, change the value to what it was.