JavaScript || Simple Hash Table Solution without Memory overflow.
Intuition\n Describe your first thoughts on how to solve this problem. \nThe trick is to increament the snap_id. if you create new array for each snapId. You\'l
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe trick is to increament the snap_id. if you create new array for each snapId. You\'ll get a memory error. You need to create an array for each snap_id IF IT\'S NECESSARY.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst, create an object which will store the arrays associated with the snap id.\nSecond, create a snap_id variable in construction function. Like so this.snap_id\n\nset(index, val): for set method check if the current array with the snap_id exists. If it does then append the value at the index of that array. If it doesn\'t exist then create a new array and add the value at the given index.\n\nsnap(): This one is the esieast. just store the current snap_id from this.snap_id in some variable. return that variable. But, before returning from the function also increament this.snap_id so we have new snap_id when we try to set some value.\n \nget(index, snap_id): this is the tricky one(but not too bad). start looping from the snap_id till 0. If we have an existing array with current snap_id then return the index at that snap_id.\n\nSOORY FOR THE LONG ANSWER.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    set: this will have time complexity of O(1)\n    snap: this will have time complexity of O(1)\n    get: this will have time complexity of O(n).\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * @param {number} length\n */\nvar SnapshotArray = function(length) {\n    this.snapId = 0;\n    this.snapShots = {};\n};\n\n/** \n * @param {number} index \n * @param {number} val\n * @return {void}\n */\nSnapshotArray.prototype.set = function(index, val) {\n    if(this.snapShots[this.snapId]) {\n        this.snapShots[this.snapId][index] = val;\n    } else {\n        this.snapShots[this.snapId] = [];\n        this.snapShots[this.snapId][index] = val;\n    }\n};\n\n/**\n * @return {number}\n */\nSnapshotArray.prototype.snap = function() {\n    const snapId = this.snapId;\n    this.snapId += 1;\n    return snapId;\n};\n\n/** \n * @param {number} index \n * @param {number} snap_id\n * @return {number}\n */\nSnapshotArray.prototype.get = function(index, snap_id) {\n\n    for(let  i = snap_id; i > -1; i--) {\n        if(this.snapShots[i] && this.snapShots[i][index] !== undefined) return this.snapShots[i][index];\n    }\n    return 0;\n};\n\n/** \n * Your SnapshotArray object will be instantiated and called as such:\n * var obj = new SnapshotArray(length)\n * obj.set(index,val)\n * var param_2 = obj.snap()\n * var param_3 = obj.get(index,snap_id)\n */\n```
['Hash Table', 'JavaScript']