SWE Interview Flashcards
(30 cards)
What is the difference between var
, let
, and const
in JavaScript?
var
is function-scoped and can be redeclared; let
is block-scoped and reassignable; const
is block-scoped and cannot be reassigned, but objects can be mutated.
How does this
work in JavaScript?
this
refers to the context of a function’s call. In global scope, it’s window
(browser). In a method, it’s the object. Arrow functions inherit this
lexically.
What is a closure in JavaScript?
A closure is a function that retains access to its outer scope’s variables. Example: function outer() { let x = 10; return () => x; }
returns a function accessing x
.
What is event delegation in JavaScript?
Event delegation handles events on a parent element for its children, using event bubbling. Example: Add click listener to a ul
to handle li
clicks (reduces listeners).
Explain the event loop in JavaScript.
The event loop processes the call stack and callback queue, handling asynchronous tasks (e.g., setTimeout
, promises) when the stack is empty, enabling non-blocking I/O.
What is a Promise in JavaScript?
A Promise represents an asynchronous operation’s outcome (pending
, fulfilled
, rejected
). Example: new Promise((resolve, reject) => resolve('Done'))
.
How do async
and await
simplify asynchronous code?
async
functions return Promises; await
pauses execution until a Promise resolves. Example: async function fetchData() { let data = await fetch(url); }
.
What is a React component?
A React component is a function or class returning UI as JSX. Example: function MyComponent() { return <div>Hello</div>; }
.
What is the purpose of useState
in React?
useState
manages state in functional components, returning a state value and updater. Example: const [count, setCount] = useState(0);
.
Explain the useEffect
hook in React.
useEffect
runs side effects (e.g., API calls) after render, with a dependency array to control execution. Example: useEffect(() => { fetchData(); }, [id]);
.
What is a controlled component in React?
A controlled component ties form input values to React state, updated via onChange
. Example: <input value={state} onChange={(e) => setState(e.target.value)} />
.
How does React.memo
improve performance?
React.memo
memoizes a functional component, preventing re-renders unless props change. Example: const Memoized = React.memo(MyComponent);
.
What is the virtual DOM in React?
The virtual DOM is an in-memory representation of the real DOM. React diffs it with the previous version to minimize DOM updates.
What is a data structure?
A data structure organizes data for efficient access/modification, like arrays (contiguous memory) or linked lists (nodes with pointers).
What is an algorithm?
An algorithm is a step-by-step procedure to solve a problem, like sorting an array or searching for an item.
What is Big O notation?
Big O describes the worst-case growth rate of an algorithm’s time or space usage as input size increases. Example: O(n) for linear time.
What is O(log n) time complexity?
O(log n) is logarithmic time, where runtime grows slowly by halving the problem. Example: Binary search on a sorted array.
What is the time complexity of accessing a hash table?
O(1) average case for hash table lookups, assuming minimal collisions and a good hash function.
What is the Sliding Window pattern, with an example?
Sliding Window optimizes subarray/substring problems. Example: function maxSum(arr, k) { let sum = 0, max = 0; for (let i = 0; i < k; i++) sum += arr[i]; max = sum; for (let i = k; i < arr.length; i++) { sum += arr[i] - arr[i-k]; max = Math.max(max, sum); } return max; }
(O(n)).
Implement Two Pointers to reverse an array in place.
```javascript
function reverseArray(arr) {
let left = 0, right = arr.length - 1;
while (left < right) {
[arr[left], arr[right]] = [arr[right], arr[left]];
left++;
right–;
}
return arr;
}
// Example: reverseArray([1, 2, 3]) → [3, 2, 1]
// Explanation: Swaps elements from both ends (O(n)).
~~~
Use Binary Search to find the first occurrence of a target.
```javascript
function firstOccurrence(arr, target) {
let left = 0, right = arr.length - 1, result = -1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
result = mid;
right = mid - 1;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
// Example: firstOccurrence([1, 2, 2, 2, 3], 2) → 1
// Explanation: Tracks first occurrence while searching (O(log n)).
~~~
What is Kadane’s Algorithm for max subarray sum?
```javascript
function maxSubArray(nums) {
let max = nums[0], current = nums[0];
for (let i = 1; i < nums.length; i++) {
current = Math.max(nums[i], current + nums[i]);
max = Math.max(max, current);
}
return max;
}
// Example: maxSubArray([-2, 1, -3, 4, -1, 2]) → 6
// Explanation: Tracks local and global max sum (O(n)).
~~~
Explain Depth-First Search (DFS) with a code snippet.
```javascript
function dfs(graph, start, visited = new Set()) {
visited.add(start);
console.log(start);
for (let neighbor of graph[start]) {
if (!visited.has(neighbor)) dfs(graph, neighbor, visited);
}
}
// Example: graph = {0:[1,2], 1:[2], 2:[]} → dfs(graph, 0) prints 0,1,2
// Explanation: Recursively explores each branch (O(V + E)).
~~~
What is the Merge Intervals pattern?
```javascript
function mergeIntervals(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
let result = [intervals[0]];
for (let i = 1; i < intervals.length; i++) {
let last = result[result.length - 1];
if (intervals[i][0] <= last[1]) {
last[1] = Math.max(last[1], intervals[i][1]);
} else {
result.push(intervals[i]);
}
}
return result;
}
// Example: mergeIntervals([[1,3], [2,6], [8,10]]) → [[1,6], [8,10]]
// Explanation: Merges overlapping intervals (O(n log n)).
~~~