I recently interviewed at Thoughtspot SMTS position.
I felt things went really smooth.
i was also able to solve a question that was given in really less time.
but i got rejected.
here is a question i was asked.
const userStore = new Store();
userStore.save("user_id_1", "samuel jackson");
const unsubscribe1 = userStore.subscribe(
"user_id_1",
(value) => {
console.log("first", value);
}, // update function
() => {
console.log("cleanup1");
} // cleanup function
);
userStore.save("user_id_1", "samuel l jackson"); //all the updaters should get called corresponding to “user_id_1”
unsubscribe1(); //should remove the update function
I was asked to design a Store class from scratch. The class needed to support three main operations:
Subscribe – This method takes a key, an update function and a cleanup function. Also, the subscribe method should return a cleanup function that can remove that specific update function when it’s no longer needed.
Save – This takes a key and a value. When a value is saved, it should notify all the update functions subscribed to that key.
Remove – This should simply remove the key from the internal cache.
class Store {
constructor() {
this.cache = {};
}
save(key, value) {
if (this.cache[key]) {
this.cache[key].value = value;
if (this.cache[key].updaters.length) {
for (let updater of this.cache[key].updaters) {
updater(value);
}
}
} else {
this.cache[key] = { value: value, updaters: [], cleanup: [] };
}
}
subscribe(key, updateFn, cleanup) {
if (!this.cache[key]) {
console.log("invalid key");
} else {
this.cache[key].updaters.push(updateFn);
this.cache[key].cleanup.push(cleanup);
}
return () => {
let cleanups = this.cache[key].updaters;
let cleanedUpdaters = cleanups.filter((c) => c !== updateFn);
this.cache[key].updaters = cleanedUpdaters;
cleanup();
};
}
remove(key) {
delete this.cache[key];
console.log(this.cache);
}
}
const userStore = new Store();
userStore.save("user_id_1", "samuel jackson");
Above was my code. it ran perfectly over all the testcases he had. The interviewer seemed happy with the code, he dint even ask me another question. The interview ended 10 minutes early.
But i got rejected. Something doesnt feel right.
Is my solution wrong?
I agree there could be some optimisations.
but he could have asked me to optimise, but he dint utter a word.
he also seemed very disinterested in the interview from the beginning.
Really a very bad experience.