Algorithms

A reference for algorithm problem-solving — the process to follow before writing any code, how to communicate your thinking, and worked examples built up over the bootcamp.

The Problem-Solving Process

Follow these steps in order — don't skip ahead to code, especially under interview pressure. The first two steps exist to make the third one easier and less error-prone.

Step What to do Key rule
1. Diagram Explain the problem in plain, layman's terms — ideally with a real-world object or analogy. No code yet. Make sure you actually understand what the problem is asking before you start thinking about how to solve it.
2. Pseudocode Write out your intended steps in plain English — not real syntax, just the logical order (e.g. "iterate through the string," "start from the back," "store each character in a new variable"). This becomes the scaffold your actual code follows. If a step is unclear in English, it'll be even more unclear in code.
3. Code Translate your pseudocode into working syntax, one step at a time. By this point the logic should already be solved — you're just writing it in the language's syntax.
Example pseudocode for reversing a string:

- take in a string as input
- create an empty variable to hold the result
- iterate through the string one character at a time
- for each character, add it to the FRONT of the result (not the end)
- once I've gone through every character, return the result

Then write the code to match — don't invent new logic at the keyboard.
                    

Interview & Communication

In a real interview, communication matters more than solving the problem. Being unable to explain your thinking — even if you're smart — is a bigger red flag than not fully solving the problem.

Why this matters:

Interviewers are assessing whether you can work on a team — not just
whether you're clever. Someone who thinks out loud, asks clarifying
questions, and explains their approach is far more valuable to a team
than someone who solves it silently with no interaction.

"I'm usually hiring people who can work on my team... not just someone
who's smart." — 99% of the time, communication matters more than raw
cleverness.

In practice:
- Ask clarifying questions BEFORE diving in — don't assume you know
  the answer already. The biggest barrier to solving a problem often
  isn't lack of skill, it's not giving yourself the chance to ask
  questions first.
- Think out loud as you work — narrate your reasoning, even if you're
  uncertain. "I'm thinking I could approach this by..." is better than
  silence.
- A partial solution with clear communication beats a full solution
  delivered in silence.
                    

The for...of Loop

A shorthand loop for iterating directly over each item in a string or array — no index variable needed. Recommended for whiteboard and interview settings: fewer moving parts means fewer chances for small syntax mistakes under pressure.

// Traditional for loop — more to write, more to get wrong:
for (let i = 0; i < str.length; i++) {
    const char = str[i];
}

// for...of — same result, much less to track:
for (const char of str) {
    // char is each character in turn, no index needed
}
Traditional for for...of
Index variable Required (let i = 0) Not needed
Condition Required (i < str.length) Not needed
Increment Required (i++) Not needed
Access current item str[i] The loop variable directly (char)
Best for When you need the index for something When you only need the value — strings, arrays

String Reversal

A classic algorithm used to introduce the problem-solving process. Two valid approaches depending on constraints, plus the cleaner interview-ready implementation.

Approach A — build a new result from the back:
Starting from the last character, take one at a time and add it to a
new empty result, moving backward through the original until you reach
the start. The implementation below does this going FORWARD through the
string, but adds each character to the FRONT of the result — same
outcome, simpler to write.

Approach B — swap in place:
Work from both ends toward the middle. Temporarily store one value so
it isn't lost when overwritten, swap the pair, then move inward until
the two sides meet. Requires no extra storage for the full string —
useful if memory is a constraint.

Which is "better" depends on constraints (available memory, whether
you can mutate the original). There's no single right answer — both
are valid strategies.
                    
function stringReverse(str) {
    let reverse = '';
    for (const char of str) {
        reverse = char + reverse;   // each char goes to the FRONT
    }
    return reverse;
}

console.log(stringReverse('CAT'));  // 'TAC'
The key trick: reverse = char + reverse

Adding `char` to the FRONT of `reverse` (not the end) is what builds
the string in reverse order as you move left to right through the
original:

  str = 'CAT'

  iteration 1: char = 'C', reverse = 'C' + '' = 'C'
  iteration 2: char = 'A', reverse = 'A' + 'C' = 'AC'
  iteration 3: char = 'T', reverse = 'T' + 'AC' = 'TAC'

If you wrote reverse = reverse + char instead, you'd just rebuild the
original string in the same order — a common mistake to watch for.