Navan interview question

interface SetAllable { get(key: string): number | undefined; // O(?) set(key: string, value: number): void; // O(?) setAll(value: number): void; // O(?) } // set('a', 5); // set('b', 2); // get('a'). ---> 5 // setAll(8); // set('a', 2); // get('a'). ---> 2 // get('b'). ---> 8 // get('c') ---> null

Interview Answer

Anonymous

10 Feb 2025

save the data as an object inside a map, the key is a string and the value is a number value and a date (the last time we set the data) when the use call the setAll function save the data with the value and the time it happened. the get returned the value of setAll in case the setAll.time happened after the key set time , else return the value of the key. the goal was to do it in O(1) and not O(n) by running and setting all the key values of the map. the interviewer told me to work with it as it was a row in the db, the time gives me the ability to know what value I need to return.