InterviewLegend

Algorithms & Simulation

Alex Chen32 min readUpdated 44 real interview reports
In this lesson
  • Expect in-house, implementation-heavy problems graded on hidden tests and inputs where brute force times out
  • Name the three archetypes the assessment reskins and the data structure each one wants
  • Drill a comment stripper, axis-aligned squares and a multi-source BFS grid, each built from an HRT problem

Most firms build a coding screen by sampling the LeetCode canon. A two-sum here, a graph clone there: problems you recognize on sight because thousands of people have solved the exact same ones. Hudson River Trading does not do that. The online assessment is a set of problems written in-house, dressed in specific mechanics, and tuned so that the obvious quadratic solution runs out of time on the hidden inputs. Almost nothing on an HRT set has a standard name you could look up, and that is by design.

That sounds harder than it is. The stories are original, but the machinery underneath is a small, stable set of shapes. Read enough of the real prompts and three keep returning. One is a stateful pass over a stream of characters or events. Another is a counting question over a set of Cartesian points. The third is a traversal over a grid. Learn to see the shape through the costume and the assessment stops being a series of surprises. It becomes three techniques you already own, applied under a clock, with edge cases that decide the verdict.

How HRT frames the assessment

The first gate for interns, new grads, and experienced engineers alike is a timed online assessment. It is typically two to four problems in a seventy-five to one-hundred-fifty minute window, on whichever platform the cycle uses. C++ and Python are both accepted, and C++ is the safe default for the software-engineer and core tracks. The set escalates in difficulty: an easy warm-up or two, an implementation-heavy middle, and a hard capstone that needs the right complexity rather than the first idea that compiles.

Two properties of the grading matter more than the problem text. First, the inputs are large on purpose, so a brute-force scan that is correct on the samples still fails the timed hidden cases. A geometry problem with a hundred thousand points wants a solution that is near-linear or n-log-n, not one that checks every quadruple. Second, passing every visible test does not mean you advance. The hidden battery probes the corners: the empty input, the single element, the value that sits exactly on a boundary. Engineers have gone one hundred percent green on the samples and still been cut because a corner case failed silently.

The Hudson River Trading angle
HRT is explicit that it steers away from burst-of-insight puzzle questions, and the assessment reflects that. The problems reward an engineer who parses a dense specification exactly, picks a data structure that fits the constraint sizes, and handles the boundaries without being reminded. That is why the archetypes below are worth more than any single solved problem. The firm is checking whether you can take an unfamiliar, precisely-worded task and turn it into correct, efficient code while the input sizes quietly rule out the lazy approach. Memorizing a named algorithm does nothing for that. Prepare for the shape, not for a bank of questions, because the questions are built fresh.

The three archetypes

Sort the real prompts by machinery and they collapse into three families. Each has a tell in the problem statement and a default structure that fits it. Spot the family first, reach for its structure, then spend your remaining time on the corners.

Archetype 1: Stateful simulation and parsing

A problem hands you a stream, a run of characters or a sequence of commands, and asks for the result of walking it once while a small amount of state changes underneath. HRT reaches for this shape often: a one-line text editor that consumes keystrokes in command and insert modes, and a source-code length calculator that counts real code while skipping comments. The default here is a single left-to-right pass with an explicit state variable. Decide the handful of states the stream can be in, decide what each input does in each state, and never scan the input twice. The trap is trying to strip or transform the stream with clever slicing instead of walking it character by character with a mode you can name.

Archetype 2: Counting and geometry over a point set

A problem gives you a list of integer coordinates and asks how many shapes they form, squares, then rectangles, or how many points cluster within a small distance of each other. The naive answer enumerates every triple or quadruple of points, which is cubic or worse and dies on the large input. The default that works is a hash set of the points for constant-time membership. Then enumerate a smaller structure, such as pairs of points that could be a diagonal or a shared side, and use the set to test for the corners that complete the shape. The whole skill is dropping the enumeration by one order: from every quadruple to every pair, with the set doing the rest.

Archetype 3: Grid traversal

A problem lays out a two-dimensional board with cells that are open, blocked, or seeded, and asks how something spreads, how far the farthest cell is, or whether every cell can be reached. It is breadth-first search on a grid, and multi-source BFS when several cells start active at once. Push every seed into the queue at distance zero and let them expand together, so the distance you record for each cell is already its distance to the nearest seed. The reachability answer falls out of the same pass, since any open cell the queue never touches is unreachable. Depth-first recursion is the tempting alternative and the wrong one, because it gives you connectivity but not the shortest spread distance.

The assessment is bespoke on the surface and predictable underneath. A parsing problem wants one pass with a named state, never a second scan. A point-set problem wants a hash set plus an enumeration one order smaller than the brute force, pairs instead of quadruples. A grid problem wants multi-source BFS, every seed enqueued at distance zero so nearest-seed distance and reachability both come free. Read the statement, match it to one of the three, and the large-input constraint that would sink a brute force is exactly what your structure was chosen to survive.

Asked at Hudson River Trading

Three problems, one per archetype, each a faithful reskin of an HRT assessment problem. They use the plain function format: the input is the function's arguments and the expected value is its return. Write the solution in the editor, run it against the hidden-style cases, then read the walkthrough, which carries the realistic escalation the rest of the way.

1

Source Length Without Comments

MediumFrequently asked
Problem
Given a string of source code, count the non-whitespace characters that are real code, ignoring everything inside comments.

Two comment forms exist, following C and C++ rules:

- A line comment starts at // and runs to the end of that line. The terminating newline is not part of the comment.
- A block comment starts at /* and runs until the first following */. Block comments do not nest, so the first */ closes the comment no matter how many /* appeared inside it. A block comment may span multiple lines.

Everything outside a comment counts if it is not whitespace. Whitespace means spaces, tabs, and newlines. A / that is not the start of // or /* is an ordinary character and counts, for example the divide in a/b.

Return the number of non-whitespace code characters.

Input. One argument, the source string. Return an integer.

Input: source = "int a = 1; // set a\nint b = 2;"

Output: 14

The two statements contribute seven non-whitespace characters each, i, n, t, a, =, 1, ; and i, n, t, b, =, 2, ; while the line comment and the newline are ignored.

How this uses the pattern

Walk the string once with a mode variable that is one of code, line-comment, or block-comment. In code mode, look one character ahead to detect the start of a comment; otherwise count the character if it is not whitespace. In a comment mode, only watch for the token that ends it.

Source Length Without Comments

Loading...
Click "Run" to execute your code

Test Cases

Input: ["int a = 1; // set a\nint b = 2;"]Expected: 14
Input: ["a/* b */c"]Expected: 2
Input: ["x /* multi\nline */ y"]Expected: 2
Input: ["a/b"]Expected: 3
Input: ["/* /* still one */ z"]Expected: 1
Input: ["ok//trailing"]Expected: 2
Input: ["//just comment"]Expected: 0
Input: [""]Expected: 0
Input: [" \t\n "]Expected: 0
Input: ["f(x); /* note */ g(y); // done\nh();"]Expected: 14
The Hudson River Trading angle
This is the archetype-one problem in its purest form: a single pass over a character stream with a tiny state machine underneath. HRT uses it because it separates people who reach for a regular expression or a chain of string replacements from people who model the states directly. Replacements break on the case that decides the problem, a // that lives inside a block comment, or a */ that appears inside a line comment and means nothing. Name the three modes, decide what each character does in each mode, and the tricky overlaps resolve themselves.
Take it further
The realistic escalation adds string literals. A // or /* inside a double-quoted string is not a comment. So the parser needs a fourth mode for being inside a string. You enter it on an unescaped quote and leave on the next unescaped quote, and a backslash escapes the character after it. Inside a string, characters count as code. The walkthrough shows the extra mode and why the escape handling has to sit in exactly one place.
2

Axis-Aligned Squares From a Point Set

MediumFrequently asked
Problem
You are given a list of distinct integer points in the plane. Count the axis-aligned squares whose four corners are all present in the list.

An axis-aligned square has its sides parallel to the axes. Its four corners are (x1, y1), (x1, y2), (x2, y1), (x2, y2) where the horizontal and vertical extents are equal and non-zero, that is x1 != x2 and abs(x1 - x2) == abs(y1 - y2). Tilted squares do not count.

The input can be large, so an approach that inspects every group of four points is too slow. Return the number of such squares.

Input. One argument, a list of [x, y] integer pairs. Return an integer.

Input: points = [[0,0],[0,1],[1,0],[1,1]]

Output: 1

The four points are the corners of the unit square, so the count is one.

How this uses the pattern

Load the points into a hash set for constant-time lookup. Enumerate unordered pairs of points and treat each pair as a possible diagonal of a square: it qualifies when the x and y gaps are equal and non-zero. Complete the square by testing the two off-diagonal corners in the set. Each square is found by both of its diagonals, so divide the total by two.

Axis-Aligned Squares From a Point Set

Loading...
Click "Run" to execute your code

Test Cases

Input: [[[0,0],[0,1],[1,0],[1,1]]]Expected: 1
Input: [[[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]]Expected: 5
Input: [[[0,0],[0,1],[1,0]]]Expected: 0
Input: [[[0,0],[0,2],[2,0],[2,2],[1,1]]]Expected: 1
Input: [[[0,0],[0,1],[1,0],[1,1],[1,2],[0,2]]]Expected: 2
Input: [[[5,5]]]Expected: 0
Input: [[[0,0],[3,0],[0,3],[3,3],[0,1],[1,0],[1,1]]]Expected: 2
Input: [[[-1,-1],[-1,1],[1,-1],[1,1]]]Expected: 1
The Hudson River Trading angle
This is the archetype-two problem, and it is the one that punishes brute force hardest. Checking every quadruple of points is quartic and hopeless on a large set, so the assessment is really asking whether you can drop an order of enumeration. A hash set of the points turns the corner test into constant time, so you can enumerate pairs instead of quadruples. Pick two points as a diagonal and the other two corners are fully determined. You just look them up. The one subtlety worth getting right on the first try is the double count, since every square has two diagonals and each finds the same square.
Take it further
The realistic escalation asks for rectangles instead of squares. A rectangle drops the equal-extent requirement: any two points with different x and different y are a valid diagonal, and the two off-diagonal corners still complete it. The pair enumeration is identical and the double-count division still applies. The walkthrough gives the one-line change and notes the row-pairing method that reaches the same count when the coordinates are dense.
3

Spreading Failure Across a Grid

MediumCommonly asked
Problem
A rectangular grid of machines is given as a matrix. Each cell holds one of three values:

- 1, a machine that has already failed at the start.
- 0, a healthy machine.
- 2, an empty rack slot that nothing passes through.

Every tick, failure spreads from each failed machine to the healthy machines directly above, below, left, and right of it. Empty slots block the spread and are never healthy machines.

Return the number of ticks until every healthy machine has failed. If some healthy machine can never be reached, return -1. If there are no healthy machines to begin with, return 0.

Input. One argument, the grid as a list of lists of integers. Return an integer.

Input: grid = [[1,0,0],[0,0,0],[0,0,1]]

Output: 2

Failure starts at two opposite corners and spreads inward. The last healthy machines to fail are two ticks from the nearest failure, so the answer is two.

How this uses the pattern

Push every starting failure into a queue at distance zero and run breadth-first search outward together. The distance recorded when a healthy machine is first reached is its distance to the nearest failure, and the answer is the largest such distance. Count the healthy cells first; if the search reaches fewer than that, some cell is walled off, so return -1.

Spreading Failure Across a Grid

Loading...
Click "Run" to execute your code

Test Cases

Input: [[[1,0,0],[0,0,0],[0,0,1]]]Expected: 2
Input: [[[1,1],[1,1]]]Expected: 0
Input: [[[1,2,0]]]Expected: -1
Input: [[[1,0,0,0]]]Expected: 3
Input: [[[0,0]]]Expected: -1
Input: [[[1,0],[2,0]]]Expected: 2
Input: [[[2,2,2]]]Expected: 0
Input: [[[1,0,2],[2,0,2],[2,0,1]]]Expected: 2
The Hudson River Trading angle
This is the archetype-three problem, and the tell is the phrase spreads to its neighbors every tick. That is multi-source breadth-first search: instead of running a separate search from each failed machine, you seed the queue with all of them at distance zero and let the frontier expand as one. The distance you stamp on a cell the first time you reach it is automatically its distance to the nearest source, so the deepest stamp is the total time. Reaching for depth-first recursion here is the classic wrong turn, because recursion tells you what is connected but not how many ticks the shortest spread takes.
Take it further
The realistic escalation lets failure also spread diagonally. With eight-directional spread, the frontier widens faster and the tick count drops, but the shape of the code barely moves: only the neighbor list grows from four offsets to eight. The walkthrough shows the change and notes that reachability can now differ, since a machine sealed off under four-directional spread may become reachable once the diagonals open up.

Key Takeaways

  • ✓HRT writes its own assessment problems rather than sampling the LeetCode canon, so recognition of named questions does not help and there is nothing to look up. The leverage is spotting the archetype under the bespoke story, then applying the structure that fits the constraint sizes.
  • ✓Three archetypes cover the recurring sets. Stateful simulation and parsing take a single pass and a named mode. Counting and geometry over a point set take a hash set and pair enumeration. Grid traversal takes multi-source BFS.
  • ✓The grading is built to defeat brute force. Inputs are large enough that a quartic point-set scan or a repeated per-source search times out. Hidden cases probe the boundaries, so a solution that passes every visible test can still be cut on a corner it never handled.
  • ✓Each drill hides a realistic escalation: string literals turn the comment stripper into a fourth mode, rectangles relax the square test to any slanted diagonal, and diagonal spread swaps four neighbor offsets for eight. Solve the base shape cleanly and the escalation is a small, named change.
Had your Hudson River Trading interview?Tell us what they asked. Reviewed before it goes in, never with your name.
Hudson River Trading interview guide

Know what Hudson River Trading asks before you walk in.

7 coding · 1 system design · 16 behavioral questions

Get Premium$49 a year · Cancel anytime
Or start freeNo card.

Get their questions first.

InterviewLegend is independent and not affiliated with or endorsed by any company it covers.