This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Python has heapq and deque. JavaScript has neither, and a few of its defaults (string sort, shift() being linear, 32-bit bitwise operators) quietly break otherwise correct answers. These are the pieces worth being able to write from memory. Every snippet here is run in the test suite with the outputs shown.
On LeetCode, ListNode and TreeNode already exist. In an interview pad they usually don't, and being able to write them in a minute, plus a way to build test input, saves you from testing in your head.
LeetCode defines this class for you on linked-list problems. Writing it out once, with the two converters, is how you test a linked-list solution locally or in a blank interview pad.
class ListNode {
val: number;
next: ListNode | null;
constructor(val = 0, next: ListNode | null = null) {
this.val = val;
this.next = next;
}
}
function listFromArray(values: number[]): ListNode | null {
const dummy = new ListNode();
let tail = dummy;
for (const value of values) {
tail.next = new ListNode(value);
tail = tail.next;
}
return dummy.next;
}
function arrayFromList(head: ListNode | null): number[] {
const values: number[] = [];
for (let node = head; node; node = node.next) values.push(node.val);
return values;
}Tree inputs on LeetCode are written level by level with null for a missing child: [3,9,20,null,null,15,7]. These two functions turn that into nodes and back, using a queue with a moving head index rather than shift().
class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val = 0, left: TreeNode | null = null, right: TreeNode | null = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function treeFromLevelOrder(values: (number | null)[]): TreeNode | null {
if (values.length === 0 || values[0] === null) return null;
const root = new TreeNode(values[0]);
const queue: TreeNode[] = [root];
let head = 0;
let i = 1;
while (i < values.length) {
const parent = queue[head++];
const left = values[i++];
if (left !== null && left !== undefined) {
parent.left = new TreeNode(left);
queue.push(parent.left);
}
const right = values[i++];
if (right !== null && right !== undefined) {
parent.right = new TreeNode(right);
queue.push(parent.right);
}
}
return root;
}
function levelOrderFromTree(root: TreeNode | null): (number | null)[] {
const out: (number | null)[] = [];
const queue: (TreeNode | null)[] = [root];
for (let head = 0; head < queue.length; head++) {
const node = queue[head];
out.push(node ? node.val : null);
if (node) queue.push(node.left, node.right);
}
while (out.length > 0 && out[out.length - 1] === null) out.pop();
return out;
}Top-k, k-way merge, Dijkstra, scheduling and running medians all need a priority queue, and the language has none. LeetCode's JavaScript and TypeScript runtime does ship @datastructures-js/priority-queue (an older major version, so check its API on LeetCode's help page), but most interview pads don't. Be able to write this in about five minutes.
An array where the parent of i sits at (i - 1) >> 1. push sifts the new item up, pop moves the last item to the root and sifts it down. Pass (a, b) => b - a for a max-heap, or compare a field for objects. Both operations are O(log n), peek is O(1).
class MinHeap<T> {
private items: T[] = [];
constructor(private compare: (a: T, b: T) => number) {}
get size(): number {
return this.items.length;
}
peek(): T | undefined {
return this.items[0];
}
push(item: T): void {
const items = this.items;
items.push(item);
let i = items.length - 1;
while (i > 0) {
const parent = (i - 1) >> 1;
if (this.compare(items[i], items[parent]) >= 0) break;
[items[i], items[parent]] = [items[parent], items[i]];
i = parent;
}
}
pop(): T | undefined {
const items = this.items;
if (items.length === 0) return undefined;
const top = items[0];
const last = items.pop() as T;
if (items.length > 0) {
items[0] = last;
let i = 0;
while (true) {
const left = 2 * i + 1;
const right = left + 1;
let smallest = i;
if (left < items.length && this.compare(items[left], items[smallest]) < 0) smallest = left;
if (right < items.length && this.compare(items[right], items[smallest]) < 0) smallest = right;
if (smallest === i) break;
[items[i], items[smallest]] = [items[smallest], items[i]];
i = smallest;
}
}
return top;
}
}Array.prototype.shift is O(n), because every remaining element moves down one. In a BFS over a large graph that turns O(V + E) into O(V²). Keep a head index instead and let the array grow.
The queue is an array you only push to. head marks the front. The memory is only reclaimed when the function returns, which is fine at interview scale.
function bfsOrder(graph: number[][], start: number): number[] {
const seen = new Set<number>([start]);
const queue = [start];
for (let head = 0; head < queue.length; head++) {
for (const next of graph[queue[head]]) {
if (!seen.has(next)) {
seen.add(next);
queue.push(next);
}
}
}
return queue;
}When the answer is per level (level order, minimum depth, rotting oranges), swap in a fresh array each round. The arrays are small and it reads clearly.
function levels(graph: number[][], start: number): number[][] {
const seen = new Set<number>([start]);
const out: number[][] = [];
let frontier = [start];
while (frontier.length > 0) {
out.push(frontier);
const next: number[] = [];
for (const node of frontier) {
for (const neighbour of graph[node]) {
if (!seen.has(neighbour)) {
seen.add(neighbour);
next.push(neighbour);
}
}
}
frontier = next;
}
return out;
}Most O(n) array solutions are a Map in disguise. Use Map rather than a plain object: keys can be numbers without being turned into strings, there's no inherited prototype to trip on, and .size is O(1).
The ?? 0 handles the first sighting. This one line is half of Top K Frequent, Valid Anagram and Majority Element.
function countAll(values: string[]): Map<string, number> {
const counts = new Map<string, number>();
for (const value of values) counts.set(value, (counts.get(value) ?? 0) + 1);
return counts;
}Group Anagrams in four lines: the sorted letters are the key. Arrays can't be Map keys by value, so join them into a string first.
function groupAnagrams(words: string[]): string[][] {
const groups = new Map<string, string[]>();
for (const word of words) {
const key = [...word].sort().join("");
const group = groups.get(key);
if (group) group.push(word);
else groups.set(key, [word]);
}
return [...groups.values()];
}When the input is lowercase English letters, an array of 26 counts beats a Map: no hashing, and two count arrays compare cheaply. charCodeAt(i) - 97 maps 'a' to 0.
function letterCounts(word: string): number[] {
const counts = new Array<number>(26).fill(0);
for (let i = 0; i < word.length; i++) counts[word.charCodeAt(i) - 97]++;
return counts;
}The single most common JavaScript bug in interviews: [10, 9, 1].sort() gives [1, 10, 9], because the default sort compares strings.
a - b ascending, b - a descending. For tuples, compare field by field with ||, which falls through to the next comparison when the first is 0.
function sortIntervals(intervals: [number, number][]): [number, number][] {
return [...intervals].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
}
function sortNumbers(values: number[]): number[] {
return [...values].sort((a, b) => a - b);
}Array(n).fill([]) puts the same array in every slot. Push into one row and every row changes.
Array.from calls the factory once per slot, so each row is its own array. fill is fine for numbers and booleans.
function grid(rows: number, cols: number): number[][] {
return Array.from({ length: rows }, () => new Array<number>(cols).fill(0));
}
function sharedRowsBug(): number[][] {
const rows: number[][] = new Array(2).fill([]);
rows[0].push(1);
return rows;
}Almost every graph problem starts with this. Remember both directions when the edges are undirected.
function adjacency(n: number, edges: [number, number][]): number[][] {
const graph: number[][] = Array.from({ length: n }, () => []);
for (const [a, b] of edges) {
graph[a].push(b);
graph[b].push(a);
}
return graph;
}JavaScript has one number type, a 64-bit float. That removes overflow worries up to 2^53 and adds a few traps of its own.
Math.floor(-7 / 2) is -4; truncating towards zero (what C, Java and LeetCode's Evaluate RPN want) is Math.trunc, giving -3. And % keeps the sign of the left side, so wrap it when you need a non-negative remainder.
function divideTowardZero(a: number, b: number): number {
return Math.trunc(a / b);
}
function mod(a: number, m: number): number {
return ((a % m) + m) % m;
}Counting problems ask for the answer mod 1_000_000_007. Reduce after every addition. Multiplying two values near 1e9 exceeds 2^53 and loses precision, so use BigInt for that one step.
const MOD = 1_000_000_007;
function mulMod(a: number, b: number): number {
return Number((BigInt(a) * BigInt(b)) % BigInt(MOD));
}Strings are immutable. s += c in a loop is usually optimised by engines, but building an array and joining once is the version nobody questions.
Split on runs of whitespace, filter out empty strings, reverse, join. Clear, and O(n).
function reverseWords(s: string): string {
return s.split(/\s+/).filter(Boolean).reverse().join(" ");
}Valid Palindrome and friends need "is this a letter or digit". A regex test per character is clear enough for an interview, and lowercase once at the start.
function isAlphanumeric(ch: string): boolean {
return /[a-z0-9]/i.test(ch);
}JavaScript engines give you roughly ten thousand stack frames. A recursive DFS on a 10^5-node linked-list-shaped tree, or memoised recursion over n = 10^5, can overflow. Know when to switch to an explicit stack or a bottom-up table.
Two indices make a string key. The function is a closure over grid and memo, which keeps the signature small. This is Unique Paths top-down.
function uniquePaths(m: number, n: number): number {
const memo = new Map<string, number>();
function paths(r: number, c: number): number {
if (r === m - 1 || c === n - 1) return 1;
const key = r + "," + c;
const cached = memo.get(key);
if (cached !== undefined) return cached;
const result = paths(r + 1, c) + paths(r, c + 1);
memo.set(key, result);
return result;
}
return paths(0, 0);
}The same traversal without the call stack. Push children right then left to visit left first, matching recursive pre-order.
function preorder(root: TreeNode | null): number[] {
const out: number[] = [];
const stack: TreeNode[] = root ? [root] : [];
while (stack.length > 0) {
const node = stack.pop() as TreeNode;
out.push(node.val);
if (node.right) stack.push(node.right);
if (node.left) stack.push(node.left);
}
return out;
}