Js Interview Prep
Js Interview Prep
This uses a sliding window approach with a Set to store seen characters.
We expand the window (right pointer) when there's no repeat, and shrink (left pointer) when we hit a repeat.
Code:
function lengthOfLongestSubstring(s) {
let maxLen = 0;
if (!set.has(s[right])) {
set.add(s[right++]);
} else {
set.delete(s[left++]);
return maxLen;
2. Debounce Function
Debouncing ensures a function runs only after a delay from the last call.
We clear the previous timer on each call and reset it, so only the last call runs after the delay.
Code:
JavaScript Interview Prep Guide
let timer;
clearTimeout(timer);
};
3. Deep Clone
This recursively clones nested objects and arrays. It checks if the value is primitive, an array, or an object,
Code:
function deepClone(obj) {
result[key] = deepClone(obj[key]);
return result;
We use recursion to handle nested arrays. Each value is checked: if it's an array, we flatten it recursively,
Code:
function flatten(arr) {
JavaScript Interview Prep Guide
if (Array.isArray(val)) {
res.push(...flatten(val));
} else {
res.push(val);
return res;
5. Rate Limiter
We track the last call timestamp and compare it to the current time.
Code:
let lastCall = 0;
lastCall = now;
return fn(...args);
};
5. What is a closure?