Skip to the notes
JSGroundwork
JSGroundwork handwritten · web dev
✎Playground→⌘Problems↻Review🔥Progress

Chapters

34 chapters
⌕
Beginner10›
B1Complexity analysisB2Arrays & stringsB3HashingB4Two pointersB5Sliding windowB6Binary searchB7Sorting algorithmsB8Stacks & queuesB9Linked listsB10Basic recursion
Intermediate12›
I1TreesI2Tree problems in depthI3Heaps & priority queuesI4Graphs: representationI5Graph problemsI6BacktrackingI7DP: 1DI8DP: 2DI9Greedy algorithmsI10IntervalsI11Bit manipulationI12Matrix problems
Advanced12›
A1Advanced DPA2Union-FindA3Advanced graph algorithmsA4Minimum Spanning TreeA5TriesA6Segment & Fenwick treesA7String algorithmsA8Monotonic stack & queueA9Design problemsA10Advanced backtrackingA11Topological patternsA12Interview strategy
/ search[ ] chaptert top

DSA in JS levels

1Beginner2Intermediate3Advanced

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
100%
A12

Interview strategy

The algorithm is half the score — the other half is everything you say before, during and after writing it.

What is actually being scored

At the advanced level the interviewer is not checking whether you can produce a correct program. They are estimating one thing: what is it like to hand this person an ambiguous problem and come back in three days? Every behaviour in this chapter is a proxy for that. Clarifying questions predict whether you will build the wrong thing. Stating a brute force before optimizing predicts whether you ship something or stall. Testing your own code predicts whether QA finds your bugs or you do. Naming a tradeoff unprompted predicts whether you will make a defensible technical decision alone.

Two candidates can both produce a working O(n log n) solution and receive "strong hire" and "no hire." The difference is almost never the code. It is that one of them narrated a decision process the interviewer could follow and trust, and the other silently emitted a memorized answer that could not be probed, extended, or debugged out loud.

clarify approach code, narrating test trade- offs 0 5 12 33 40 45 min no code is written before minute 12 — that is deliberate, not slow the common failure: coding at minute 3 → wrong complexity discovered at minute 30, no time to recover, and → the interviewer never saw you reason, only saw you type
The first twelve minutes buy the last thirty. A wrong approach caught at minute 8 costs nothing; the same mistake found at minute 30 ends the interview.

The first five minutes: questions that change the answer

Silence here is the single strongest negative signal available, and it is the cheapest to fix. An engineer who starts coding from an under-specified problem statement is telling the interviewer exactly how they behave with an under-specified ticket. But not all questions are equal — "can I use a hashmap?" wastes the goodwill you are trying to build. Ask only questions whose answer would change your solution, and say why you are asking.

AskWhy it changes the solution
"How large can n get?"The highest-value question in the interview. It fixes your target complexity before you have written anything — see the table below.
"Is the input sorted, or can I sort it?"Sorted unlocks two pointers and binary search for free. If not sorted, an O(n log n) sort may be free anyway when the target is already O(n log n) — but it is not free if the target is O(n).
"Can there be duplicates?"Changes whether you dedupe, whether a Set is safe, whether two-pointer needs a skip loop, and whether the expected output is unique.
"What's the range of the values? Negative? Zero? Floats?"Negatives break sliding-window-on-sums and greedy arguments. Small bounded values unlock counting sort or a bitmask. Floats kill exact equality.
"What should I return for empty input / no valid answer?"An explicit contract, decided up front, instead of an ad-hoc guess at minute 40.
"Can I mutate the input?"In-place sorting or marking may be the whole space optimization; if the input is shared state, it isn't allowed.
"Is this called once, or repeatedly on the same data?"Repeated queries change the answer entirely — preprocess into a prefix array, segment tree, or index map and amortize.
"Does the whole input fit in memory, or is it a stream?"Streaming rules out sorting and random access; pushes toward heaps, reservoir sampling, count-min sketch.
Say it like this → "Before I start — a few things that would change my approach. How big is n? Can values be negative? And are duplicates possible in the input? …n is up to 10⁵ and values can be negative — good, that rules out the sliding-window approach I was about to reach for, since a negative number means the window sum isn't monotonic."

That last clause is the part that scores. You are not collecting facts, you are demonstrating that each fact eliminates a branch of your decision tree. Two or three questions asked this way beat ten asked mechanically.

⚠ Don't ask questions you can answer from the examples If the provided example contains a negative number, asking "can values be negative?" reads as not having read the problem. Skim the examples first, extract what they already settle, and ask only about what they leave open — then say so: "the example has duplicates so I'll assume they're allowed; what I can't tell from it is whether the array is guaranteed non-empty."

A framework that works on a problem you have never seen

You will not recognize the problem. That is the point of an advanced interview. What you need is not recall but a procedure that visibly makes progress even from zero. Run these seven steps out loud, in order.

#StepWhat you actually say
1Restate in your own words"So: given X, return Y, where the constraint is Z. Is that right?"
2Work the given example by handSay the answer for the example before writing anything. Catches misreads instantly.
3State a brute force, with its complexity"The obvious thing is to check every pair — O(n²) time, O(1) space. That's my baseline; let me see if I can beat it."
4Name the bottleneck"The expensive part is that for each i, I re-scan everything before i. That inner scan is the thing to remove."
5Read the constraints for the target"n goes to 10⁵, so O(n²) is 10¹⁰ — far too slow. They're steering me to O(n log n) or O(n)."
6Ask what structure removes the bottleneck"What would make that inner scan O(1) or O(log n)? A hashmap of seen values, a heap, a monotonic stack, or precomputed prefix sums."
7Confirm, then code"So: one pass, hashmap from value to index, O(n) time and O(n) space. Shall I code that?"

Step 3 is non-negotiable and candidates skip it constantly, believing a brute force looks weak. The opposite is true: it guarantees you have a solution on the board within five minutes, it proves you understand the problem, and it gives you a concrete complexity to improve on. An interviewer will almost always let you skip implementing it. What they will not forgive is twenty minutes of silence hunting for the clever answer.

brute force O(n²), stated aloud what work repeats? "I re-scan the prefix" which structure answers it in O(1)? hashmap · prefix sums · heap · monotonic stack · sorted order + two pointers · binary search on the answer · union-find · trie · memoized state optimization is a search over this middle box, not over memorized solutions
Almost every optimization in interview DSA is the same move: identify work being redone, then buy it back with a data structure.

Constraints → intended complexity: the highest-leverage table here

Interviewers and problem setters choose n deliberately. The bound is a hint about the intended solution, and reading it correctly can collapse a twenty-minute search into thirty seconds. Calibrate against the rough industry rule that ~10⁸ simple operations is about one second.

Constraint on nIntended complexityPattern family it points at
n ≤ 10-12O(n!) · O(n! · n)Full permutation search, brute-force TSP, "try every ordering"
n ≤ 20-25O(2ⁿ) · O(2ⁿ · n)Bitmask — subset enumeration, bitmask DP, meet-in-the-middle (2^(n/2)) if n ≈ 40
n ≤ 100O(n³) · O(n⁴)Floyd-Warshall, interval/matrix-chain DP, triple nested loops are fine
n ≤ 1,000-5,000O(n²)2D DP over pairs — edit distance, LCS, palindromic substrings; all-pairs comparison
n ≤ 10⁵O(n log n)Sort-then-scan, heap, binary search on the answer, balanced BST / ordered set, divide and conquer, segment tree
n ≤ 10⁶-10⁷O(n) · O(n log log n)Single pass, two pointers, sliding window, hashmap, counting sort, prefix sums, sieve, Kadane
n ≤ 10⁹-10¹⁸O(log n) · O(√n) · O(1)Math/closed form, binary search over the answer space, matrix exponentiation, digit DP — you cannot even read the input

Read the second half of the constraints too. "Sum of all string lengths ≤ 10⁵" over many strings means linear in the total, which points at a Trie or Aho-Corasick rather than per-string work. A value range like "values ≤ 100" alongside a huge n points at counting/bucketing. A memory limit matters as much as time: n = 10⁵ with an O(n²) DP table is 10¹⁰ cells — impossible regardless of the time limit, which tells you the DP must be rolled down to one or two rows.

Read the constraints backwards Do not design a solution and then check whether it is fast enough. Read n first, derive the complexity the setter intends, and use that as a filter on which patterns are even eligible. "n ≤ 20" is not trivia — it is the interviewer telling you the answer involves subsets. Very few candidates do this, and it is visible in seconds when someone does.
Say it like this → "n is at most 20, which is a strong hint — 2²⁰ is about a million, so exponential in n is affordable and polynomial probably isn't achievable here. That points at enumerating subsets with a bitmask, most likely bitmask DP over the set of already-used elements. Let me check whether the state really is just 'which subset is used' or whether I need an index too."

How to talk while you code

The goal is a continuous, low-effort narration that lets the interviewer follow your reasoning without interrupting. Not a play-by-play of syntax — nobody needs "now I'll write a for loop." Narrate decisions, and name your patterns, because naming is what proves the choice was deliberate rather than lucky.

Instead ofSay
silence"I'll use a monotonic decreasing stack here, because I need the next greater element for every index and a stack lets each element be pushed and popped once — amortized O(n)."
"now a map""Map from value to index rather than a Set, because I need to return indices, not just detect membership."
"hmm, hold on""I'm deciding between sorting first and using a heap. Sorting is simpler but O(n log n) up front; the heap gets me the top k in O(n log k). Since k is small I'll take the heap."
"…" while fixing a bug"That should be <=, not < — otherwise the last window never gets evaluated. Let me re-check the boundary."
"I'll handle that later""I'm deliberately deferring the empty-input case; noting it here as a TODO and I'll come back before I call this done."

Two mechanical habits pay for themselves. First, write the function signature and the return type before the body — it forces the contract to be explicit. Second, when you defer something, say so and leave a visible marker; an acknowledged gap is a plan, an unacknowledged one is a bug.

⚠ Narrating and thinking are different modes, and forcing both at once stalls people It is entirely fine to say "give me twenty seconds to think this through quietly" and then go silent — that reads as controlled. What reads badly is undeclared silence for two minutes. Buy the quiet explicitly, use it, then come back with a statement rather than a mumble.

Being stuck, handled well

You will get stuck. It is expected and it is not disqualifying — freezing is. What is being measured is whether you have a procedure for it. Work this ladder out loud, in order, and say which rung you are on.

RungDo thisWhy it works
1Re-read the constraints and the exact wording of the askMost stuckness is a misread — "subsequence" vs "subarray," "any" vs "all," "at most" vs "exactly." The constraint bound also re-states the target complexity you may have drifted from.
2Work a tiny example by hand — n = 1, n = 2, n = 3You are looking for the rule your hand is following. Solving n = 3 manually and asking "what did I just do?" recovers the recurrence more often than staring at the general case.
3Ask "what shape is this?"Not "have I seen this problem" but: is it a graph? intervals? a tree of choices? a search over a monotonic answer space? Shape recall is far more reliable than problem recall.
4Ask whether it is two known patterns composedAdvanced problems usually are. Sort + two pointers. Trie + backtracking. Binary search on the answer + a greedy feasibility check. Heap + hashmap. Topological order + DP. Say the two names out loud.
5Relax the problem, solve the easier versionDrop a constraint (assume sorted, assume no duplicates, assume k = 1, assume the array is positive). Solve that, then re-introduce the constraint and see what breaks. Partial credit is real credit.
6State where you are stuck, precisely, and take the hint"I have an O(n²) solution and I know the bottleneck is re-scanning the prefix; what I can't see is a structure that gives me the max of a shrinking window in O(1)." That is a targeted request, and it lets the interviewer give a small hint rather than a large one.
Say it like this → "I'm going to slow down for a second and work n = 3 by hand, because I think the recurrence will be obvious once I see what I'm doing manually. …Right — at each step I'm choosing between taking this element and skipping it, and the choice only depends on the remaining capacity. That's a knapsack shape, so the state is (index, remaining) and I can memoize it."

Note what that phrasing does: it converts "stuck" into "executing a deliberate step." Interviewers are instructed to give hints; taking one gracefully costs far less than most candidates fear, and refusing to ask while burning ten minutes costs far more.

Test before you say "done"

Declaring completion and letting the interviewer find the bug is the most avoidable score loss in the entire interview. Finding it yourself, out loud, converts the same bug into a positive signal. Trace the given example first — line by line, tracking real variable values, not vibes — then run a deliberately chosen edge case.

Edge caseWhat it catches
Empty input [] / ""arr[0] on an empty array, Math.max() of nothing returning -Infinity, a while loop that assumed one element
Single elementTwo-pointer and sliding-window loops whose body never executes; left < right vs left <= right
All duplicates [5,5,5,5]Dedupe logic, Set-vs-Map choices, two-pointer skip loops, "distinct" requirements
Already sorted / reverse sortedWorst-case quicksort partitioning, degenerate BSTs, and off-by-one at the array ends
Two elementsThe smallest case where a comparison or swap can be backwards
Negatives and zeroGreedy and sliding-window sum arguments that silently assume positivity; division and modulo by zero
All elements identical to the target / none matchingThe "not found" return contract you agreed on in minute two
Maximum n from the constraintsRecursion depth (JS blows up around 10⁴-10⁵ frames), integer overflow past 2⁵³, O(n²) memory
Say it like this → "Let me trace the given example before I call this done. left = 0, right = 4, sum = 9, target is 9 — returns [0, 4], matches. Now an edge case: single element. The while (left < right) never executes, so I fall through to the not-found return — correct. And an empty array: nums.length - 1 is -1, the loop still doesn't execute, still correct. I'm happy with this."
⚠ Tracing "in your head" is not tracing Under pressure, silently re-reading code confirms what you intended to write, not what you wrote. Say the actual variable values out loud, or write them in a comment block. The whole value of the exercise comes from forcing yourself to evaluate rather than recognize — and it is also the only way the interviewer can see you doing it.

Discussing tradeoffs at a senior level

The clearest seniority marker in the last ten minutes is raising a tradeoff before you are asked, and framing it against a use case rather than in the abstract. Intermediate candidates report complexity. Advanced candidates report complexity, name the alternative they rejected, and say what would change their mind.

AxisThe sentence to have ready
Time vs space"This is O(n) time with an O(n) hashmap. If memory were the binding constraint I'd sort in place and use two pointers — O(1) extra space, O(n log n) time. Which matters more here?"
Preprocess vs per-query"If this is called once, the linear scan is right. If it's called a million times on the same array, I'd build prefix sums up front — O(n) once, then O(1) per query."
Worst case vs average case"Quickselect is O(n) expected but O(n²) adversarially. If this is on a user-facing path where input could be hostile, I'd take the heap's guaranteed O(n log k) instead."
Simplicity vs constant factor"The bitmask version is maybe 5× faster per node but noticeably harder to read. For n ≤ 12 I'd ship the readable one and leave a comment about the optimization."
Amortized vs bounded latency"The dynamic array is amortized O(1) but a single resize is O(n). If this were in a real-time path I'd pre-size it."
Exact vs approximate"For exact distinct counts I need O(n) memory. If an error of a percent or two is acceptable at this scale, HyperLogLog does it in kilobytes."
Mutating vs pure"I'm sorting the input in place, which is faster but destroys the caller's array. If it's shared, I'd copy first and pay the O(n)."

Every one of those ends in a question or a condition. That is deliberate — it turns a monologue into a design conversation and invites the interviewer to supply real-world context, which is the exact interaction they are trying to score. It is also honest: which side of a tradeoff is correct genuinely depends on information you do not have.

Two more things to have ready without being asked. How would this scale past one machine? — even a sentence ("if the array doesn't fit in memory, I'd external-sort by chunk, or shard by hash of the key and merge") shows the thinking extends past the whiteboard. And what would you test? — naming three unit tests and one property ("the output should always be a permutation of the input") signals engineering maturity that pure DSA never reaches.

Signals that separate an advanced performance from an intermediate one

  • Constraints are read as a hint, not as trivia. "n ≤ 20, so they intend an exponential-in-n solution — that means bitmask" is said in the first two minutes, not discovered at minute thirty.
  • A brute force is stated with its complexity before any optimizing starts. There is always something on the board, and the improvement is measured against a named baseline rather than asserted.
  • Patterns are named out loud as they are chosen. "Monotonic stack, because I need the next greater element and each index is pushed and popped once" — not just correct code that happens to be a monotonic stack.
  • The bottleneck is identified explicitly ("the expensive part is re-scanning the prefix") and the optimization is presented as buying that specific work back with a specific structure.
  • Getting stuck produces a described procedure, not silence. Re-read constraints → hand-trace a tiny case → identify the shape → check for two composed patterns → relax a constraint → ask a precise question.
  • The candidate tests their own code before declaring done — a real trace with real values, plus a deliberately chosen edge case, and finds their own off-by-one.
  • Tradeoffs are raised unprompted and tied to a use case, with a stated condition that would flip the decision, rather than a memorized complexity table recited on request.
  • Corrections are absorbed without defensiveness. A hint is taken, integrated, and credited — "good catch, that breaks when the values are negative; let me fix the invariant" — because the interviewer is simulating what code review with you feels like.
  • Uncertainty is stated honestly and bounded. "I'm fairly sure this is O(n log n) amortized but I'd want to double-check the resize cost" beats a confident wrong claim every time, and it is the difference between an engineer you can trust and one you have to verify.
Practice this layer

Opens in the editor — write it, run it, and check it against real tests.

Read the Constraint, Pick the Complexity4 tests · intermediateWhich Approaches Actually Fit?5 tests · intermediate
←previousTopological patterns↑ Cover