0% found this document useful (0 votes)
2 views4 pages

Js Interview Prep

The document is a JavaScript interview preparation guide that covers key topics such as finding the longest substring without repeating characters, implementing a debounce function, deep cloning objects, flattening nested arrays, and creating a rate limiter. It also includes flashcard answers explaining concepts like 'this' in JavaScript, the event loop, hoisting, differences between map and forEach, and closures. Each topic is accompanied by code examples to illustrate the concepts effectively.

Uploaded by

Dibyendu Biswas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Js Interview Prep

The document is a JavaScript interview preparation guide that covers key topics such as finding the longest substring without repeating characters, implementing a debounce function, deep cloning objects, flattening nested arrays, and creating a rate limiter. It also includes flashcard answers explaining concepts like 'this' in JavaScript, the event loop, hoisting, differences between map and forEach, and closures. Each topic is accompanied by code examples to illustrate the concepts effectively.

Uploaded by

Dibyendu Biswas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

JavaScript Interview Prep Guide

1. Longest Substring Without Repeating Characters

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.

This keeps track of the longest window of unique characters.

Code:

function lengthOfLongestSubstring(s) {

let left = 0, right = 0;

let set = new Set();

let maxLen = 0;

while (right < s.length) {

if (!set.has(s[right])) {

set.add(s[right++]);

maxLen = Math.max(maxLen, right - left);

} else {

set.delete(s[left++]);

return maxLen;

2. Debounce Function

Debouncing ensures a function runs only after a delay from the last call.

It's useful in limiting high-frequency events like keystrokes or resizes.

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

function debounce(fn, delay) {

let timer;

return function (...args) {

clearTimeout(timer);

timer = setTimeout(() => fn.apply(this, args), delay);

};

3. Deep Clone

This recursively clones nested objects and arrays. It checks if the value is primitive, an array, or an object,

and makes a new copy at each level.

Code:

function deepClone(obj) {

if (obj === null || typeof obj !== 'object') return obj;

if (Array.isArray(obj)) return obj.map(deepClone);

const result = {};

for (let key in obj) {

result[key] = deepClone(obj[key]);

return result;

4. Flatten Nested Array

We use recursion to handle nested arrays. Each value is checked: if it's an array, we flatten it recursively,

otherwise we add it to the result.

Code:

function flatten(arr) {
JavaScript Interview Prep Guide

const res = [];

for (let val of arr) {

if (Array.isArray(val)) {

res.push(...flatten(val));

} else {

res.push(val);

return res;

5. Rate Limiter

This limits a function to run only once every 'limit' milliseconds.

We track the last call timestamp and compare it to the current time.

Code:

function rateLimiter(fn, limit) {

let lastCall = 0;

return function (...args) {

const now = Date.now();

if (now - lastCall >= limit) {

lastCall = now;

return fn(...args);

};

JavaScript Flashcard Answers


JavaScript Interview Prep Guide

1. What is `this` in JS?

- In a regular function, `this` is dynamic and refers to the caller.

- In arrow functions, `this` is lexically bound (inherits from parent scope).

2. What is the Event Loop?

- The event loop manages execution of JS code, handling async callbacks.

- Microtasks (Promises) run before macrotasks (setTimeout).

3. Hoisting of `var` vs `let`:

- `var` is hoisted and initialized to `undefined`.

- `let`/`const` are hoisted but not initialized (temporal dead zone).

4. Difference between `map()` and `forEach()`?

- `map()` returns a new array with transformed values.

- `forEach()` executes a callback for each item but returns undefined.

5. What is a closure?

- A closure is a function that remembers the scope where it was created.

- Used in debounce/throttle to maintain state across calls.

You might also like