method

The first parser I wrote for a scan report worked on my sample and died on the second file The sample had clean lines The real file had an empty field, a username with a colon, and one line that ended mid record A green exit code is not evidence the parser was right It only means the compiler stopped arguing

This track uses C++ for the implementations When you study algorithms in a language that hides allocation, you learn the shape but not the cost C++ makes you pay attention to both That is the point, not masochism

The learning curve

  • stage one , you cannot see the problem shape
  • stage two , you see a shape but reach for the wrong tool
  • stage three , you reach for the right tool and leak memory doing it
  • stage four , you test against ugly input and fix the contract
  • stage five , you explain why the solution works without running it

Most people stop at stage three Stage three is where the demo works and the tool dies in production

Step one, restate the problem

If the prompt says “find the first repeated host in a list”, your restatement might be: Given a sequence of host strings, return the earliest value that appeared before, or nothing

You just decided what “first” means and what “value” means Ambiguity you resolve now costs nothing Ambiguity you discover after writing eighty lines costs an evening and a cold coffee

Step two, write the contract

input  : vector of strings , may be empty , may contain duplicates
output : optional string
errors : malformed entries are still strings , not exceptions

std::optional says “maybe there is an answer” without inventing a sentinel value Returning an empty string for “not found” is how you confuse a valid empty hostname with failure

Step three, hand walk

Take alpha , beta , alpha

  • alpha , nothing seen before
  • beta , nothing seen before
  • alpha again , that is the answer

Now [] Now alpha Now alpha , alpha

Those four cases define more of the program than the prompt did

Step four, compile the idea

#include <optional>
#include <string>
#include <unordered_set>
#include <vector>

std::optional<std::string> first_repeat(const std::vector<std::string> &items) {
    std::unordered_set<std::string> seen;
    for (const auto &item : items) {
        if (seen.contains(item)) return item;
        seen.insert(item);
    }
    return std::nullopt;
}

The set holds exactly the values before the current position That is the proof, one sentence

Step five, test like you mean it

assert(first_repeat({"alpha", "beta", "alpha"}) == std::optional<std::string>("alpha"));
assert(!first_repeat({}).has_value());
assert(!first_repeat({"alpha"}).has_value());

Compile with strict flags A warning in C++ is the compiler asking politely before it stops being polite

Step six, explain it

The set contains the previous values The first time a value is already there, it is the earliest repeat No later repeat can be earlier

If you cannot say that without looking at the code, you do not own the solution yet

Mistakes that cost me time

  • treating empty input as an error when the contract says nothing
  • using a vector for membership and wondering why large inputs crawl
  • changing the contract halfway and not updating the assertions
  • returning a sentinel value that is also valid input

Exercises

  1. Restate “smallest port greater than a given number” without reusing its words
  2. Write input and output types for a function that finds the most common hostname
  3. Hand walk first_repeat with beta , alpha , beta , gamma
  4. Compile the example with -Wall -Wextra -Werror and read every warning

What to remember

The hand example is the cheapest debugger you will ever have The compiler is the second cheapest Production is the expensive one

Posted