New
Review
Cards from the stages you've passed. Each comes back when the chance you remember it has fallen to about 90%.
Loading your cards…
No cards yet. Each stage you pass adds its cards here.
Why clear tool results before anything else when a conversation goes over its budget?
They're most of its tokens, the model has already read them and replied from them, and a cleared one can be seen again by calling the tool.
What does clearing a tool result keep, and why?
The call and the result's place, with its id: a tool message must follow the call it answers, and the model still sees what it called and how to see it again.
Why keep what the caller said in notes, rather than trust the summary to keep it?
A summary is the model's choice of what matters, written again at each compaction; a note stays word for word in the system message, through every summary after it.
Why does a summary take whole turns, from a caller's message on?
A turn's tool calls and results belong together. A cut inside one leaves a result without its call, which the server refuses.
What does a researcher keep out of the agent's conversation?
Its searches: the passages it read and its own calls stay in its own conversation, and the agent gets only its short answer and the ids it cites.
Why is a researcher's last request sent without tools?
So that the model has to answer from what it has found, instead of searching again and running out of requests.
Why does a test of a memory's summary need turns of different lengths?
A cut by a count of messages lands on a turn's start only when the turns are the same length; a turn with two tool calls moves it inside the turn.
What goes wrong when a conversation's budget counts only each request's prompt?
The reply goes out with the next request too, so a long one takes the conversation over its budget unnoticed.
Why shouldn't the desk name a caller's notes file by their id as it is?
The id is the caller's input: one like ../a names another caller's file, or one outside the folder. A hash of the id gives each caller one file inside it.
Why cap the notes a caller can hold?
Every note goes into every request of every conversation the caller has after it, so a cap keeps what they cost in tokens bounded.
What do pass@k and pass^k each measure of an agent?
pass@k, the chance that at least one of k trials of a task does it; pass^k, the chance that all k do. pass@k rises with k and pass^k falls.
Why score a help desk's agent by pass^k rather than pass@k?
Each caller gets one conversation, so what matters is that the agent does the task every time, not that one of several tries does.
Why take each task's median over its trials before a percentile over the tasks?
A task's trials aren't independent: a slow task is slow on every trial. Pooled, its trials count as separate draws, and the interval comes out narrower than the data allow.
What makes the order-statistic interval for a percentile hold whatever the costs' spread?
Each value falls below the true p-quantile with the chance p, so the count below it is binomial with n and p, whatever the distribution. The ends are the sorted values at the counts that leave a chance of a or less on each side.
Why is a chat request's span of kind CLIENT, and a tool's INTERNAL?
The request goes over HTTP to another process, the model's server. The tool runs in the agent's own process.
What goes in a span's error.type, and why not the error's message?
A kind of error with few values, such as the status code "503" or an exception's class name, so errors can be counted by kind. The message goes in the span status's description.
Why is (c/n)^k a poor estimate of a task's pass^k from n trials?
It draws each trial again after using it, so a task gets a chance of k in a row though no k of its trials were all done. C(c, k) / C(n, k) draws without replacement, and is unbiased.
What z gives a two-sided interval at a confidence, and what does inv_cdf(confidence) give instead?
inv_cdf((1 + confidence) / 2), which leaves half of what the interval leaves out beyond each end. inv_cdf(confidence) is a one-sided bound's z: two-sided, at 95% it holds 90%.
Why compare two agents' pass^k task by task rather than by their two intervals?
Most of each score's spread is between easy and hard tasks, which the two agents share. The differences on each task cancel it, so their interval is narrower and fits the question asked.
When does a release gate hold a candidate for reliability with a tolerance t?
When the low end of the interval for its pass^k less the current agent's is below -t: the runs can't rule out that it's worse by more than t. A better candidate ships.
Why does the desk's search show each passage as [p17] beside the instruction to cite?
The model writes its reply from what it reads: with each id in the form the reply has to cite it, the citation is already in its context.
What does a tool's error give a model that called it wrongly?
What was wrong and what would work, such as the tools there are, the values a field allows or an example of the arguments, so its next call can fit.
Why does the desk make a ticket or a callback in code, on the caller's yes, rather than when the model calls a tool?
A rule in the prompt is one the model can miss; a write only code makes, after the yes, can't happen before it, whatever the model does.
What does propose_callback look up before the caller is asked, and why?
The caller's open ticket for the service and a free time that fits, so the caller is asked about a call the desk can make, and the model hears what to ask when it can't.
When the caller says yes to a proposal, what does the model read, and when?
The desk makes the proposal first, in code; the model then reads the caller's message with what the desk did, and replies from that.
Why does a message that doesn't say yes drop the desk's pending proposal?
A yes counts only for the proposal it answers. Kept, a proposal could be made by a yes to a later question, which the caller never meant for it.
A yes comes after another booking took the proposed time. What should the desk do?
Check the time before writing anything, make nothing, and say the times that are free: opening the call's new ticket first would leave a ticket with no call.
Why did tests that checked the state after each yes miss a call booked before the yes?
Booked early or on the yes, the state at the end was the same. Only a check of the state between the proposal and the yes, or after a no, sees the difference.
Why does a move book the new time before it cancels the old one?
If the booking fails, the old call is still there: the caller is never left with no call.
What should a refused move tell the model?
What the caller can ask for instead: the calls they have booked, or the times that are free, so the model's next question is one the desk can answer.
Why does an agent's tool give an error as its result instead of raising?
The model reads the result, so it can do something about the error, such as ask for the free times and offer one. A tool that raises leaves the program to decide what the caller hears.
What does a tool call's dispatcher check before it runs the tool?
That the tool exists, that the arguments are a JSON object, and that they give every argument the tool requires and none it doesn't take, so a model's mistake comes back as an error it can read.
In a workflow, who decides what happens after the model's reply?
Code. The model does one thing at a time, such as sorting a message or answering from passages, and code follows a fixed path for each kind of message.
How does the desk's workflow make sure a callback waits for the caller's yes?
It keeps the offer, and books it only when the caller's next message is a yes. The model never calls a tool, so it can't book anything itself.
What does an agent's loop send back after a reply that calls tools?
The model's message with its calls, then each call's result in a tool message with the call's id, in the order of the calls, and then it asks the model again.
Why does an agent keep the whole conversation, tool calls and results included, from turn to turn?
The API keeps nothing between requests: a caller's yes means something only next to the offer it answers, and the tickets the agent looked up are only in the results it was sent.
What does a chat API refuse when an agent sends tool results without the model's message that called the tools?
The results: a tool message must follow the assistant message whose tool_calls it answers, by the call's id.
Why does an agent that starts each of the caller's turns afresh fail at a confirmation?
The API keeps nothing between requests, so the caller's yes reaches the model without the offer it answers.
What does a router between a workflow and an agent send to each?
The workflow gets the messages whose route it can follow, in few requests and with its rules in code; the agent gets the rest, which no fixed path fits.
Why does the desk's router sort a conversation only by its first message?
Each path keeps its own conversation: the workflow waits for a yes to its offer, and the agent holds the messages and tool results so far. Sorting again would drop them.
What does each request to the chat completions API carry?
The whole conversation so far: the API keeps nothing between requests, and the model reads all of it again.
Which value of finish_reason says that a reply was cut off at its token limit?
length. A reply that finished its answer has stop, and one that calls a tool has tool_calls.
Which error statuses get the same answer when the same request is sent again?
400, 401, 403 and 404: the request, the key or the path has to change first. A 429, 500 or 503 may pass.
Why does the model read more tokens for a message than the message's own words take?
Its chat template wraps every message in special tokens that mark where it starts and whose it is, and starts the reply with tokens of its own.
What do a conversation's tokens have to fit in, besides the prompt?
The reply's tokens: the server's window holds both, so a client leaves room for the reply.
When a conversation is too long, which messages go first?
The oldest whole turns, each a customer's message with its replies. The system message stays.
Which tokens does OpenAI's prompt_tokens include that Anthropic's input_tokens leave out?
Those read from the cache and those written to it: OpenAI counts them inside prompt_tokens, and Anthropic reports them beside input_tokens.
A model charges more for prompts over a limit. Which tokens count toward the limit?
All the prompt's tokens, those read from the cache and written to it included, and the higher prices then apply to every token of that request.
Why does a month's cost come from pricing each request, not the month's total tokens?
Prices can depend on each request's prompt length, so one long request pays more on its own, without changing the price of the others.
Where should the message of an error from the model's API go?
To the shop's logs. The customer gets a message they can act on, such as calling the shop.
Which replies may a customer be shown?
Only those whose finish_reason is stop: a reply cut off at length, or one that calls a tool, isn't an answer.
What role does the model's earlier reply take when the conversation is sent again?
assistant. Sent back as user, the model reads it as the customer's words.
Which failures of a request to a model's API may pass when it's sent again?
A 408, 409, 429 or any 5xx, a timeout and a lost connection. A 400, 401, 403 or 404 gets the same answer again.
Why are a client's retry waits multiplied by a random factor?
To spread out clients that failed together, which would otherwise all ask again at the same moments.
How do a request's timeout and a deadline differ?
The timeout limits one attempt. The deadline limits every attempt and wait together, so the last attempt gets only the time left.
Where should a tool that answers for the signed-in customer get the customer from?
From the shop's sign-in, held by the code. Taken from the model's arguments, it's whatever the model was talked into writing.
Why should another customer's booking read exactly like a missing one?
So no answer reveals anything about bookings the customer can't see, not even that they exist.
What does the reference B-2001' OR '1'='1 do to SQL built by formatting it in?
It closes the string and adds a condition every row meets. Passed as a parameter, ?, it's only a value to compare.
Why does the tool the model calls to cancel a booking only propose it?
Anything the model reads can talk it into a call. The customer confirms with a button the model can't press.
Why is a pending action's id random, rather than built from its booking?
Whoever has the id can try to confirm the action, so it mustn't be guessable: secrets.token_urlsafe(16) gives 16 random bytes.
Why does the chat window show no image from a model's reply?
A browser fetches an image as soon as it's shown, so an address the model was told to write can send the customer's details out without a click.
Why check a link's address with a WHATWG URL parser rather than urllib.parse?
The check must read the address as the browser will. urlsplit reads https://evil.example\@bikes.example/ as bikes.example; a browser goes to evil.example.
When does a link in a model's reply match the shop's host?
Only when its host equals one of the shop's exactly. bikes.example.evil.example starts with the shop's name and is someone else's.
What should a tool's result hold?
Only what the answer needs. The model may repeat any of it to whoever it's talking to.
Why shouldn't a model's instructions hold a discount code, a key or a password?
A model can be talked into repeating its instructions, so anything in them can reach any customer.
What does testing an assistant against a model that obeys every injection show?
That each guarantee holds in code: whatever the model calls or writes, no other customer's data, no unconfirmed action, no unsafe reply and no spending past a limit.
Where can a prompt injection reach a shop's assistant from?
From anything the model reads: the customer's own message, and any text a tool returns, such as reviews or notes others wrote.
What should a tool result say when the database fails?
Only that the records can't be read. Table names, SQL and error messages describe the shop's internals, which the model may repeat.
Does {"type": "integer"} accept 3.0? And true?
3.0, yes: an integer is any number with no fractional part. true, no: JSON's booleans aren't numbers, though Python's True == 1.
What does "format": "date" check, by default?
Nothing: format is a note for the application unless the validator is set to check it, so a date that doesn't exist passes. Check dates in code.
Which strings does "pattern": "[0-9]{4}" accept?
Any with four digits in a row anywhere in it: a pattern isn't anchored unless it starts with ^ and ends with $.
An object's schema has properties but no additionalProperties. Which other keys does it accept?
Any, with any values. "additionalProperties": false refuses keys properties doesn't name.
A reply fails the schema. Why does it go back to the model as the assistant's message, with its errors after it?
The model then reads what it wrote and what's wrong with it. The same request sent again tends to get the same reply.
What does OpenAI's strict mode ask of every object in a schema?
Every property listed in required, and "additionalProperties": false. A field that may be empty is required too, with null among its types.
A reply's finish_reason is length. Should the code read its JSON?
No: the reply was cut off at its token limit, and what the model meant to write next is missing.
A server holds a reply to a schema while the model writes it. Can the reply still fail the schema?
Yes. A reply cut off at its token limit is the start of a valid reply, not a whole one, and isn't valid JSON.
What does constrained decoding do with a token that can't lead to a reply the schema allows?
Takes it out before the model's choice: llama.cpp sets its score to minus infinity. The end of the reply stays out until the reply is whole.
The model scores highest a token the schema doesn't allow. What does it write?
The allowed token it scores highest, which may mean something else: the schema turns what the model would have said into a reply that fits.
A schema says "format": "date". What should the code do with the date before using it?
Parse it, with date.fromisoformat, and treat a ValueError as a reply it can't use: format checks nothing by default.
What does Python's date(2026, 10, 25).weekday() give for a Sunday, and what does isoweekday() give?
6 and 7: weekday() counts Monday as 0, and isoweekday() counts it as 1.
Which rules belong in a reply's schema, and which in the code that reads the reply?
The schema holds the reply's shape: keys, types, lists of values, patterns. The code holds what depends on the world: that a date exists, hasn't passed, and falls on an open day.
Why does a form a model fills in need a way to say no?
A model held to a form fills it in whatever the input says: without a value such as null, an email that asks for no booking still becomes one.
What does a model see of a tool before it calls it?
Its name, its description and the JSON Schema of its parameters, so those are written for the model to read.
A tool call's arguments don't parse, or name a parameter the function doesn't have. What should the code do?
Send the model the error as the call's result, so it can call again. Raising ends the conversation instead.
In strict mode, how is a parameter that can be left empty described?
As required, with null among its types: strict mode requires every property.
After a reply with tool calls, what goes in the next request, besides the conversation so far?
The model's message with its calls, then a tool message for each call, in the order of the calls, each with its tool_call_id.
Why does a tool loop cap its rounds?
Each round is another request to the model, which a customer waits on, and a model can keep calling tools without ever answering.
In a streamed reply, when can the code parse a tool call's arguments?
Once the reply has finished: the arguments come in pieces of JSON that don't parse until the last one has come.
Where does a server-sent events line end?
At CR LF, at LF, or at CR, and nowhere else. Splitting text as str.splitlines does also splits at characters such as U+2028.
How does a streamed reply say how many tokens it used?
With "stream_options": {"include_usage": true}, in a last chunk whose choices is empty, before data: [DONE].
A model's reply asks for two tool calls. How many does the code run?
Both, sending each result back with its call's id: a reply can call several tools at once, and each call needs its result.
Why does the code look up a tool call's name only among the tools it offered the model?
A tool call is text the model wrote after reading the customer's message. Looked up anywhere else, its name can run any function the code can reach.
An MCP tool call fails on the arguments the model gave. Does the server answer with a JSON-RPC error or a result?
A result, with isError true and text that says what went wrong, for the model to read. JSON-RPC errors are for requests that are wrong in themselves, such as a call to a tool the server doesn't have.
How does an MCP server of version 2026-07-28 know which version a request uses?
From the request's own _meta, which carries io.modelcontextprotocol/protocolVersion and the client's capabilities. There's no handshake: each request stands on its own.
Which JSON-RPC messages get no reply?
Notifications: messages without an id.
What is training–serving skew?
A model given its inputs one way in training and another in service, so the same case gets different inputs, and a different answer, with no error to show it.
Why can sending a category's codes as text instead of numbers change a model's answer without an error?
An encoder that ignores values it hasn't seen treats "7" as unknown when it learned 7, and encodes the stay as having no value there at all.
How do you keep a model's inputs the same in training and in service?
Make them in one function that both paths call, from the fields a case carries.
Why score a model against a baseline?
A score means little alone: the baseline, here ranking patients by their inpatient visits in the year before, is what the users have without the model, and the model has to beat it to be worth serving.
Why fit the scaler and the encoder only on the training part of a split?
So nothing the model is given, not even a mean or a spread, comes from the stays it's tested on.
What does stratifying a split by the label do?
It keeps the label's share the same in both parts, so the test isn't made easier or harder by how the rare cases fell.
Why can one held-out split mislead you about a model?
Its score depends on which cases happened to be held out: here, ten draws of the same split gave AUCs from 0.604 to 0.646 for one model.
What is the standard error of a cross-validated mean?
The fold scores' standard deviation over the square root of the number of folds: a guide to how far the mean could be from the model's true score.
Why compare models on the same folds?
So both meet the same cases, and a difference between them comes from the models rather than from the draw.
Why check calibration on out-of-fold probabilities?
A model scored on the cases it learned from looks better than it will on new ones; each out-of-fold probability comes from a model that never saw its case.
What does a calibration table compare?
For groups of cases ranked by probability, each group's mean probability with the share of it that turned out positive.
Why read a Brier score against the base rate's?
For a rare outcome most of the score comes from the rarity itself, so giving everyone the base rate already scores low: here 0.0831, against the model's 0.0818.
Two models' cross-validated AUCs differ by less than the noise. Which should ship?
The simpler one: the folds show no gain from the other, and the simpler one costs less to run and explain.
What does class_weight="balanced" do to a logistic regression's probabilities for a rare outcome?
It pushes them far too high, though the ranking barely moves: here they add up to 3,247 readmissions where 640 came back.
What does handle_unknown="ignore" make a one-hot encoder do with a category it didn't learn?
Give it all zeros instead of raising an error, so the model answers from the other inputs.
How does a share of stays to call set the threshold?
The threshold is the probabilities' 1 - share quantile, so the share with the highest probabilities lie at or above it.
What happens to recall and precision as the planners call a larger share?
Recall rises and precision falls: here 20.3% at 18.6% for 10%, and 33.6% at 15.4% for 20%.
What does calling at random reach, on a cumulative gains chart?
The diagonal: calling a share of the stays reaches the same share of the readmissions.
Why measure the recall and precision to report on out-of-fold probabilities?
Scored on the stays it learned from, a model rates itself better than it is: here a recall of 23.8%, where new stays get 20.8%.
Why isn't the threshold for calling a share of the stays the share itself?
The threshold is a probability and the share is a part of the stays: set at 10%, the threshold would call 29% of them. The threshold is the probabilities' 1 - share quantile.
Once cross-validation has scored a model, which stays should the shipped model learn from?
Every one: held-out stays score a model, and the one that ships learns from all there are.
Why does the service load a saved model instead of training when it starts?
It answers with the model whose numbers were measured, it starts without a fit, and it doesn't need the extract.
Can a model saved with one version of scikit-learn be loaded with another?
It may load, but scikit-learn doesn't support it: the results can differ or the process can crash. Train it again with the versions running.
Why load a joblib file only if you made it?
joblib saves with pickle, and loading a pickle can run any code the file holds.
What does FastAPI answer to a request that doesn't fit the endpoint's pydantic model?
A 422, before the endpoint runs, whose detail names each field that's missing or can't be read.
What does a FastAPI endpoint's return type, such as -> Prediction, do?
FastAPI checks each answer against it and describes the answer in the API's schema.
Why does app.py load the saved model as it's imported, rather than in the endpoint?
So the model is read once, and each request only encodes a row and scores it.
Why score a batch of stays with one call to predict_proba, rather than one call per stay?
The pipeline scales, encodes and weighs the whole table at once, so a batch costs little more than a single stay.
Why does the batch endpoint limit how many stays a request can bring?
To bound each request's work: while one request is scored, no other on the event loop can run. A client with more stays sends more batches.
How does FastAPI's 422 for a batch say which stay is wrong?
Its loc gives the field's path through the request, with the stay's place in the list.
Why read the day's discharges with keep_default_na=False?
pandas reads None as a missing value by default, and here None is an HbA1c test not done.
Which rows does day.iloc[start : start + n] take?
The n rows from start, up to but not including start + n, where the next batch starts.
Why leave the deaths and hospice care out of a batch before sending it?
The service refuses a whole batch with one of them in it, so the other stays get no answer.
What does the live share called, called over answered, tell the team?
Whether the service still calls about the share it was set to. A drift says the patients, their coding or the model have changed.
Why time a service as the median over many requests?
Any one request's time varies with what else the machine is doing; the median says what a typical request takes.
Why should the service refuse to start on a model trained with other versions?
scikit-learn doesn't support loading it, and a service that started anyway could give different answers without any error.
How can you tell whether your tests would catch a bug?
Plant the bug in the code and run them again: at least one should fail.
Why compare each answer in a batch with that stay's own probability?
A batch answered out of order still gives valid answers. Only matching each stay to its own answer shows the mix-up.
Why test a batch of exactly the limit, and not only one over it?
A limit off by one refuses the full batch, and only a test at the limit itself catches it.
Why work out every number in a model's write-up with code rather than typing it?
A model trained again gets a write-up that matches it, and no number goes stale.
Why give a model's AUC with its standard error, and beside a simple baseline?
The error says how far the number would move with other stays, and the baseline says what a ranking with no model already reaches.
What belongs in a write-up's section on what the model doesn't know?
The limits of what it learned from: whose stays, from when, and the stays it doesn't answer for.
Why serve a model inside a scikit-learn pipeline with its encoding steps?
The pipeline encodes each request with what it learned from the training rows, so a request is encoded exactly as the training rows were.
What does FastAPI answer for a request body that doesn't fit the endpoint's pydantic model?
A 422, with a detail that names each field that's missing or can't be read.
Why does a prediction service train or load its model once, when it starts, rather than in each request?
Training takes far longer than one prediction, and every request needs the same model, so each request would wait for work already done.
With totals[i] the sum of the first i numbers, what is sum(numbers[start:stop])?
totals[stop] - totals[start]: one subtraction, however long the range.
Going through a share's prices once, what do you keep to find the best single trade?
The lowest price so far: the best sale on each day buys at it.
What is the largest sum of a run ending at a number, given the largest sum of a run ending just before it?
The larger of the number alone and the number added to that sum.
Why does the largest run start its best sum at minus infinity rather than 0?
A run holds at least one number, so a list of negative numbers must give its largest number, not 0.
How do you find the product of every number but one, for each number, without dividing?
Multiply the product of the numbers before it by the product of the numbers after it, each built in one pass.
Reading a grid in a spiral, ring by ring, when does a ring skip its bottom row or its left column?
A ring of one row skips its bottom row, and a ring of one column its left column: each would read values its top row or right column already read.
Binary searching for the first number not smaller than a value, what holds either side of the bounds low and high?
Every number before low is smaller than the value, and every number from high on is not.
How do two binary searches find the first and last positions of a value in a sorted list?
One finds the first number not smaller than the value, the other the first number larger: the last position is one before it.
Binary searching a sorted list that was rotated, what do you check at each middle?
Which half is in order, from its two ends, and whether the value lies between them: if so search that half, else the other.
Why find a whole-number square root by binary search rather than with int(math.sqrt(n))?
A float holds only about 16 significant digits, so a large n gets a wrong root; squaring whole numbers stays exact.
Why can a binary search find the least capacity that ships packages within some days?
A larger capacity never needs more days, so the capacities that work are all those from the least one up.
One pass with a dict finds two numbers that add up to a target. What does it look up for each number, and where?
target - number, among the numbers before it, kept in a dict from each number to its position.
How do you find the first character that appears only once in a text?
Count every character in a first pass, then go through the text again and stop at the first character with a count of one.
What key in a dict gathers each word with its anagrams?
The word's letters in sorted order, which anagrams share and other words don't.
Finding the longest streak of consecutive integers with a set, which numbers do you count up from?
Only those whose predecessor isn't in the set, so that each streak is walked once.
Counting runs that add up to a target with prefix sums, what do you look up for each number?
How many earlier prefix sums equal total - target, where total is the sum so far.
Why does the counter of prefix sums start as {0: 1}?
The sum before any number is 0, so a run that starts at the first number is counted too.
Merging k sorted lists with a heap, what does the heap hold?
The next number of each list with numbers left, with its list and position, so the smallest is always on top.
Finding the anagrams of a word in a text with a sliding window, what does the window keep, and when has it found one?
How many of each character it still needs, and how many are missing in all: none missing at the word's length is an anagram.
How does a window of k numbers move one step and keep its sum, in one step?
Add the number that comes in and take away the one that drops out.
Finding the longest run with no character twice, where does the window start when the new character is already in it?
Just after that character's last position, so only the earlier copy drops out.
Why can a window that grows and shrinks find the shortest run reaching a total only when every number is positive?
Then growing always raises the sum and shrinking lowers it, so a dropped start never needs to come back.
How does a sliding window keep track of how many different values it holds?
Count each value in a dict, and remove a value when its count reaches zero: the dict's length is the number of values.
Finding the smallest window that covers some letters, when can the window's start move on?
While the character there is one the window has to spare: one it holds more of than it needs, or doesn't need at all.
Merging intervals sorted by start, when does the next interval join the last merged one, and what's the merged end?
When it starts at or before that one's end; the merged end is the larger of the two ends.
How do sorted start and end times give the fewest meeting rooms?
Go through the starts in order, freeing a room for every end at or before each one: the most rooms in use at once is the answer.
How does a heap find the kth largest of n numbers in time in proportion to n log k?
Keep a min-heap of the k largest so far: a number larger than its smallest replaces it, and at the end its smallest is the kth largest.
To write the largest number from a list side by side, in what order do two numbers a and b go?
a first when str(a) + str(b) is larger than str(b) + str(a).
Finding the most frequent items, how do you keep items that tie in the order they first appear?
Count them in a dict, which keeps that order, then sort by count: Python's sort is stable, so ties stay in order.
Checking that brackets are balanced with a stack, what must a closing bracket find, and what must be left at the end?
Its own kind of opening bracket on top of the stack, and nothing left once the text ends.
Evaluating reverse Polish notation with a stack, which value popped first is the right-hand operand?
The first popped: for 7 2 -, the stack pops 2, then 7, and computes 7 - 2.
In days until warmer, what does the stack hold, and in what order?
The days still waiting for a warmer one, never warmer towards the top, so a warm day settles them from the top down.
How can a stack give its minimum in constant time?
Push each value with the minimum of the stack up to it: the top's minimum is the stack's, and popping uncovers the one before.
Decoding 3[ab] with a stack, what waits on the stack at the [?
The text built so far and the count, until the ] adds the part, repeated, back to that text.
How do you check that a text's letters and digits read the same both ways, without copying the text?
Move one position in from each end, step past other characters, and compare what they stop at, ignoring case, until they meet.
Which two words of a list share exactly the prefix that every word shares?
The alphabetically first and last, min(words) and max(words): every other word sorts between them.
Decoding "x12y3" one character at a time, how do you know where x's length ends?
Read digits until the next character that isn't one: the texts hold no digits, so it starts the next run.
Why does checking that two texts follow the same pattern need a dict in each direction?
One catches a character renamed two ways, and the other two characters renamed to the same one.
Finding the longest palindrome by widening from each middle, what are a text's middles?
Each character, for odd lengths, and each gap between two characters, for even ones.
In a sorted list, two positions move inwards to find a pair that adds up to a target. Why can the left one move on when the sum is too small?
The right number is the largest still in play, so the left number has no partner left.
Removing repeats from a sorted list in place, how do you tell that a value is new?
It differs from the last value written, since equal values sit together in a sorted list.
Finding the most water two lines can hold with two positions moving inwards, which line moves, and why?
The shorter one: with any line closer in, it holds at most its height times a smaller gap, so it can't do better.
How does sorting first find the triples that add up to zero in time in proportion to n² rather than n³?
Fix each first number, then find the pairs after it that add up to minus it with two positions moving inwards, in one pass.
What sets the water above a bar after rain?
The lower of the highest bar on its left and the highest on its right, minus its own height.
With two positions moving in from the ends, why can you settle the side whose bar is lower?
The other side has a bar at least as high, so the water there depends only on the highest bar on its own side.
Two rows tie for first. What do rank(), dense_rank() and row_number() give the row after them?
3, 2 and 3: rank() skips past a tie, dense_rank() doesn't, and row_number() gives the tied rows 1 and 2 in no set order.
avg(trips) OVER (ORDER BY hour) in Postgres: the mean of which rows?
Every row from the first to the current one, with its peers: the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. A mean of the last three hours takes ROWS BETWEEN 2 PRECEDING AND CURRENT ROW.
In a GROUP BY ROLLUP (borough, payment) query, how can you tell a borough's subtotal from a row whose borough is NULL in the data?
GROUPING(borough, payment): 0 for a row grouped by both, 1 for a borough's subtotal, 3 for the grand total, whatever the columns hold.
Which rows does WHERE borough <> 'EWR' keep when borough can be NULL, and what keeps the rest?
Only rows whose borough is set and isn't 'EWR': NULL <> 'EWR' is NULL, which WHERE drops. borough IS DISTINCT FROM 'EWR' keeps the NULLs too.
A job deletes a day's rows, then inserts them again, on an autocommit connection. What can go wrong, and what fixes it?
If the insert fails, the day is left empty, and readers see it empty while the insert runs. Doing both in one transaction makes them one all-or-nothing change.
Two DataFrames have the same columns in different orders. What does union do with them, and what does unionByName do?
union matches the columns by position, so each value lands in whatever column sits in its place. unionByName matches them by name.
A trip has no drop-off time. What is F.col("dropoff_at") < F.col("pickup_at") for it, and what is ~ of that?
Both are null: a comparison with a missing value is missing, and so is its negation. F.coalesce(flag, F.lit(False)) makes the flag false.
A zone's daily counts skip the days it had no trips. Which frame gives the seven days ending each day: rowsBetween(-6, 0) or rangeBetween(-6, 0) over a day number?
rangeBetween(-6, 0), which takes the rows within 6 of the current day's number. rowsBetween takes the last seven rows, whatever days they fall on.
Why divide a seven-day sum by 7 rather than take avg over the frame, when some days have no row?
avg divides by the rows the frame holds, so a missing day drops out instead of counting as 0.
What does F.count(F.when(F.col("payment_type") == 1, 1).otherwise(0)) count, and what counts the card trips?
Every row, since .otherwise(0) gives each one a value. F.count_if(F.col( "payment_type") == 1) counts the rows where the condition is true.
congestion_surcharge is missing on some trips. How do F.sum(a + b) and F.sum(a) + F.sum(b) differ?
a + b is missing when a is, so F.sum(a + b) drops those rows' b too. F.sum(a) + F.sum(b) sums each column over the values it has.
A job rewrites a day's Parquet folder with mode("overwrite") and fails partway. What is left, and how do you keep the old files until the new ones are complete?
The old files are gone. Write the new folder elsewhere on the same disk, then rename it into place once it's written.
Spark reads trips/day=2025-08-05/ as a column day. What type is it, and why cast it?
Spark infers it from the name, a date here, and uses a string when type inference is off. A cast makes it a date either way.
Spark reads a CSV file with the header option set and nothing else. What type does each column have?
A string. inferSchema is off by default, so the code casts each column it needs.
A GHCN-Daily day's rain reads 0, with the measurement flag T. What does it mean?
A trace: rain fell, too little to measure. The day isn't missing, and it isn't dry.
A daily table counts each borough's trips, then joins the weather to the counts. Which rows does it lack?
Every borough and day without a trip. A count makes no row for nothing, and the join keeps only rows that match. Build the grid of every day and borough first, then left-join the counts to it.
After a left join, which missing values should become 0, and which should stay missing?
A count that found nothing becomes 0. A value that was never measured, such as a day's weather, stays missing.
Why compare a wet Wednesday with dry Wednesdays rather than with every dry day?
Riding changes with the weekday, and wet days don't fall evenly through the week. Comparing within a weekday keeps the weekday out of the difference.
A window has partitionBy and no orderBy. Which rows does an aggregate over it see?
Every row of the partition. With no ordering, the frame runs from the partition's first row to its last.
F.count("passengers") and F.count("*") over the same trips give different numbers. Why?
F.count of a column counts the rows where it isn't missing. F.count("*") counts every row, whatever its values.
What does Spark's dayofweek give a Sunday, and a Saturday?
1 for a Sunday, 7 for a Saturday. F.date_format(day, "EEEE") gives the day's name.
A wet day needs at least 1 mm of rain. Which input tests that rule, and why that one?
A day with exactly 1.0 mm. Only the edge tells >= 1.0 from > 1.0, and real data may never land on it.
Why build a test's input with spark.createDataFrame rather than read a month of real data?
A few rows show one requirement, with a result you can work out by hand, and run in a moment. Real data tests only what it happens to hold.
A booking call times out. Why can't the agent just send it again?
The booking may have happened. An idempotency key makes a second send book once, and the agent reads the patient's appointments before it tries again.
Why make every tool call as the signed-in patient?
Then the booking system itself refuses anything outside that patient's appointments, whatever the agent is told.
Why should a review bot post few comments rather than every finding?
A bot that's often wrong teaches engineers to ignore it, so it ranks its findings and posts only the most severe few.
What number shows whether a review bot's comments are worth reading?
The share of its comments that led to a change in the code.
Why decide which invoices staff check by rules, not by the model's confidence?
A model's confidence isn't a measure of whether a field is right. Rules such as a failed check, a new supplier or a large amount are the firm's to set and to see.
What identifies a duplicate invoice?
The same supplier, invoice number and total, from whichever channel it came.
How does an SQL assistant make two managers' "last month's sales" agree?
It builds on the warehouse's agreed definitions, kept as views, rather than on raw tables, so both answers count sales the same way.
Why keep personal data from an SQL assistant with the database role rather than the prompt?
The role's access can't be talked around, so no query it runs can return those columns, whatever the question says.
A support assistant's answer cites none of the passages it was given. What should happen to it?
It goes to staff instead of to the customer, since an answer that cites nothing may not come from the help pages.
Why give a support assistant no tool that issues refunds?
Then a message that tells it to give a refund can only change what it says, and refunds stay with staff.
Why filter by access inside the search, not after it?
Taking the best passages first and dropping the forbidden ones can leave a person on few matters with almost nothing, and a ranking that saw forbidden passages can reveal them.
A document's access changes but its text doesn't. What does its index need?
Only the groups on its passages rewritten. Its embeddings stay as they are.
Why should a call's summary be structured output rather than a paragraph?
The weekly view counts problems by category and outcome, which needs fields code can validate and add up, not free text.
What does a queue give a pipeline that has an hour to summarise each call?
It smooths the day's peaks, rides out short failures with retries, and holds the calls through an outage until they can be cleared.
Where should a shopping assistant's prices come from?
From the catalogue, through a tool, in the same turn. The model never states a price it wasn't given.
Why does a shopping assistant propose basket changes rather than make them?
A wrong change costs the customer effort to undo, so each change shows the product, size, quantity and price, and applies only when they confirm it.
Why give batch work its own, lower-priority rate limit in a model gateway?
Batch work can wait, so it uses what the products leave over instead of starving them.
Which requests can a model gateway cache by exact match?
Only those with one right answer, such as a classification, and only with an expiry, since an answer that depends on changing data goes out of date.
Why compare a prompt change with the current release question by question?
Each question's own difficulty drops out of its pair's difference, so the gate can find a smaller drop with the same questions.
What does a canary see that a test set doesn't?
Real users' questions, of every kind, which a test set can only sample.
How do you set the threshold at which a moderation model hides posts?
On labelled posts, so that the posts it hides fit what the volunteers can review in a day.
Why should a moderation model hide posts rather than delete them?
A wrong hide costs its writer a wait, which a volunteer can undo. A wrong delete loses the post.
What limits how many people a model on your own GPUs can serve at once?
Usually the KV cache, which grows with every token each conversation holds, not the weights.
Why choose the smallest model that clears the bar for a customer's own servers?
A smaller model leaves more GPU memory for the cache, so more conversations fit at once.
Why render Granite Guardian's prompt from its own chat template?
So that the prompt is the one the model was trained on, word for word, and changes with the model's own template.
Why read the grader's probability of Yes rather than its one-word answer?
A probability is a score: it can be ranked against people's labels at every threshold, and set to the threshold the help desk chooses.
Why choose a grader's threshold on the development set and report it on the test set?
A threshold chosen on the test set reports the best agreement that set allows, not what the grader will do on the next replies.
What does Cohen's kappa take out of a grader's accuracy?
The agreement that chance alone would reach if the grader and the people each gave 1 at their own rate.
Why check a grader on your own pages when it already agrees with people on a benchmark?
A threshold carries its false alarms to new text unmeasured: replies of another kind, from longer passages, can be flagged far more often than the benchmark's were.
How can you make replies whose verdict is known without labelling any?
Give a reply written from its own passages a context it can't keep to, such as another service's pages. Passages from the same service often say the same thing, so they make unsound labels.
Why can't a grader's scores be cached under the reply alone?
The verdict is on the reply and its context together, so the same reply with other passages needs a grade of its own.
What should a grading run do when the grader's server refuses a request?
Stop and raise. A refusal counted as a verdict passes replies no one graded, and the run looks like one without failures.
A grader flags most of a release's replies, and many replies that keep to their passages too. What can the report still do?
Rank the replies by risk for people to read from the top, and compare releases on the grader's scores, question by question, rather than on the share it flags.
Why doesn't the share of replies a grader flags say how many of them are wrong?
The grader also flags replies that keep to their passages: 0.444 of people's replies with their own. Its flags say where people start reading.
Why do more replies to each question stop narrowing a release's standard error after a few?
Sampling's part of a question mean's variance falls as one over the number of replies, but the question's own part doesn't fall at all. Past a few replies, only more questions narrow it.
Two releases answered the same questions. Which standard error does their difference in score take?
The standard error of each question's difference, each page's together. Adding the two releases' standard errors treats them as if they had answered different questions.
Why does a change to the help desk's search need its margin clustered by page more than a change to its prompt does?
A page's questions are shown the same passages, so a change to the search moves them together. A change to the prompt moves each question on its own.
What is the smallest drop a one-sided test at level alpha finds with a given power?
The standard error of the change times the sum of the normal quantiles at one less alpha and at the power.
How many more questions does a test set need to find a drop half the size it finds now?
Four times as many: the standard error falls with the square root of the number of questions.
Why does a release gate that stops any candidate scoring lower than the baseline fail reruns of the release it guards?
A rerun's score moves by chance, and comes out lower about half the time. Stop only when the change's interval lies wholly below zero.
Why does a gate compare two releases question by question rather than score by score?
A question one release finds hard, the other mostly does too. Each question's difference takes that shared part out, and the margin narrows.
A release gate checks three scores. At what level does it test each, so that a release no worse than the baseline is stopped at most alpha of the time?
At alpha over three. The chance that any of the three stops it is at most the sum of their levels.
What does a release gate's exit status do in CI?
A status of 1 fails the job, so the release stops there; 0 lets it go on to ship.
Why does a split of the help desk's questions keep each page's questions on one side?
Questions about one page are answered from the same text, so a page on both sides lets what was tuned on the development set pass for a gain on the test set.
Why are questions with the same words dropped before the split, not after?
One on each side would score the test set on a question the development set tuned for, and one counted twice weighs twice.
Why split each service's questions by the same share?
So that each service keeps its share on both sides, and a change that helps one service and hurts another is measured on each.
What can a check written in code tell about a help desk's reply, and what can't it?
Whether it cites passages, which ones, whether it ended by itself and how long it is. Not whether what it says is in the passages it cites.
Of the help desk's test replies, what share cite a passage that holds the answer, and why are the rest not all the model's fault?
0.515. The search showed an answer for 0.812 of the questions, and no reply can cite an answer it wasn't shown.
Why does a test set whose pages hold several questions each need a clustered standard error?
A page's questions pass and fail together, so they aren't independent. Treating each alone makes the margin too narrow.
What does a design effect of 1.17 say about the help desk's 165 test questions?
Its clustered variance is 1.17 times the independent one, so the set tells as much as 141 independent questions.
Why can a test set drawn with a fixed seed still change from one run to the next?
A set of strings comes out in an order that changes with each Python process, so pages listed from a set are drawn in another order. List them in a fixed order first.
What goes wrong when a test set stops at exactly its share, partway through a page?
The page's other questions stay in the development set, so what is tuned on them passes for a gain on the test set.
How many more questions does a test set need to halve its margin of error?
Four times as many: the margin falls as one over the square root of the questions.
Why does the help desk decide on the share over every question, not each service's?
Each service's margin is too wide: it would need 235 to 500 questions for ±0.05, where it has 26 to 55.
A token bucket with a burst of 5 and a rate of 1 a second has been idle for an hour. How many requests at once does it let through?
- It gains a token a second, but holds no more than its burst.
Why does the bucket's Wait unlock its mutex before it waits for its timer?
Holding the lock while it waited would make every other caller of the bucket wait behind it, for the whole wait.
Bucket holds a sync.Mutex. Why do its methods take a *Bucket, not a Bucket?
A mutex must not be copied after first use. A value receiver would lock a copy, which guards nothing.
Why add random jitter to exponential backoff?
Clients that failed together would otherwise retry together, in spikes. Random waits spread their retries out over time.
Retry's next wait would end after the caller's deadline. Why does it give up at once rather than wait?
The call after the wait couldn't finish in time, so waiting would only spend the caller's remaining time before giving the same error.
How does a channel with a buffer of 3 keep a gateway to 3 calls at once?
Each call sends into it before it starts and receives from it when it's done. With 3 values in the buffer, the next send waits until a call finishes.
In ChatAll, why can each goroutine write replies[i] without a lock?
Each writes a different element, so none write the same memory, and WaitGroup.Wait returns only after every goroutine has returned, with its write visible.
A function starts a goroutine per upstream, each sending its answer on a channel with no buffer, and returns after the first answer. What happens to the other goroutines?
Each waits forever on its send, as nothing receives again. Goroutines aren't garbage collected, so each request leaks them.
asking, cancel := context.WithCancel(ctx) is made and its cancel deferred. What does the cancel stop if the calls were given ctx?
Nothing: only work given asking, or a context made from it, sees the cancel.
In an outage where every call fails, why does a per-client retry budget matter when each request already stops at three attempts?
Three attempts each still nearly triples the load on servers that are failing from too much of it. A budget of retries as a share of requests holds the growth near a tenth.
Why does the retry budget have a floor as well as a ratio?
A client that has sent few requests would get almost no retries from a ratio alone. The floor lets a quiet or new client retry.
Why does the budget count requests and retries over a window rather than since it began?
An outage's retries leave the window once it's over, so the budget recovers. Counted forever, one outage would hold back retries for as long as the gateway ran.
Why does the gateway's handler check the key and the rate limit before it reads the body?
A caller without a key, or over its limit, is refused for the cost of reading a header: no body read, no upstream asked.
Why does the handler pass r.Context() to Fallback?
It's canceled when the caller's connection closes, so the upstreams stop working on a reply no one will read.
A handler sets a header after it writes the body. What does the caller get?
The reply without that header: the first write sends the status and the headers, and changes to the header map after it have no effect.
What does a Go program lose if it doesn't close an HTTP response's body?
The connection: the transport may not reuse it, so each call opens a new one, until the program runs out of them.
http.Client's Timeout is zero. How long can a call take?
As long as the request's context allows: a zero Timeout means no timeout, so with a context that has no deadline, a stuck server holds the call forever.
A server-sent event stream ends without data: [DONE]. What should a reader of OpenAI's stream format do with the text it has?
Treat it as an error: the stream was cut short, and the text so far would look like a whole reply.
Why can Relay answer a failure with an error status only before the first piece?
Flushing the first piece sends the status and headers. After it, changing them has no effect, so a failure can only end the stream.
What does a server-sent event line that starts with a colon mean?
It's a comment, which a reader ignores. Servers send them to keep a quiet connection open.
A cache's Chat holds its mutex while it calls the upstream. What does that do to the gateway?
Every request through the cache, cached or not, waits for that one call, so the gateway answers one request at a time.
Why does a cache of chat replies key on the whole request, not the last message?
The reply depends on every message. Conversations that end with the same message would otherwise get each other's replies.
What does singleflight's Do add to a cache?
Only one call is in flight for a key at a time: callers who ask for the same key at once wait for that call and get its result, so the upstream is asked once.
What timeouts does a Go http.Server have if you set none?
None: a zero timeout means no timeout, so a slow or silent client holds its connection for as long as it likes.
Why must a program wait for Shutdown to return, rather than for Serve?
Serve returns ErrServerClosed as soon as the shutdown starts. A program that exits then cuts off the requests Shutdown is still waiting for.
The context that started a shutdown is already done. Why not pass it to Shutdown?
Shutdown gives up when its context is done, so it would return at once without waiting for anything. It needs a context of its own, with the grace as its deadline.
Why does the gateway check errors.Is(err, ErrNoModel) rather than err == ErrNoModel?
== matches only the error itself. errors.Is also finds ErrNoModel when it's wrapped inside another error, as code that passes it up with %w will.
A Go struct has a field MaxTokens int with no tag. What does encoding/json fill it with from {"max_tokens": 200}?
Nothing: json matches a key to a field's name exactly or ignoring case, and max_tokens isn't MaxTokens either way, so the key is ignored and the field stays 0.
What does fmt.Errorf("%w: %w", ErrNotJSON, err) let a caller find?
Both errors: errors.Is finds ErrNotJSON, and errors.AsType finds the type of err, such as json's *SyntaxError.
In Go, what happens when code reads a key from a nil map, and when it assigns one?
Reading gives the zero value of the map's element type. Assigning causes a run-time panic, so a map is made, with make or a literal, before anything is added to it.
Why does For return slices.Clone(upstreams) rather than upstreams?
A slice shares its array with the one it came from. A caller that changed an element of the routes' own slice would change that model's route for every request after it.
Why does Models sort the map's keys before it returns them?
Go doesn't specify the order of a map's iteration, and it can change from one iteration to the next, so unsorted keys would come back in a different order each time.
The tests' fake upstream has func (f *fake) Chat(...). Why does the list of upstreams hold &fake{...} rather than fake{...}?
Pointer methods can only be invoked on pointers, so only *fake has every method of Upstream. The pointer also lets Chat count its calls in the fake the test holds.
Why does Fallback go on to the next upstream after a 503, but stop at a 400?
A 5xx is that server's own trouble, which another may not share. A 4xx says the client erred, so every upstream would refuse the same request.
What does ctx.Err() give before a context's deadline, and after it?
nil before; context.DeadlineExceeded after, or context.Canceled if it was canceled instead.
var err error comes before a for loop, and inside it, key, err := parse(line). Which err does the code after the loop see?
The one declared before the loop, unchanged: the loop's body is a block of its own, so := there declares a new err that only the loop can see.
Why does err == ErrOverLimit miss an error made with fmt.Errorf("key %s: %w", key, ErrOverLimit)?
The wrapping error is a different value, and == compares only that value. errors.Is looks through the wrapping and finds ErrOverLimit.
An upstream answers 500 with a message naming its address. Why does the gateway answer its caller with its own message instead?
An upstream's message can give away its address, keys or software. The caller gets a message that gives no details, and the details go to the gateway's own logs.
An upstream asks callers to wait 3.1 seconds. What Retry-After does the gateway send, and why not 3?
Retry-Aftertakes whole seconds, and rounded down, the caller would come back before the upstream is ready.
Why does WriteError set Content-Type before it calls w.WriteHeader?
The headers go out with the status, so changing them after WriteHeader, or after the first Write, has no effect.
How do you list every balanced string of n pairs of brackets?
Build each a bracket at a time: add ( while fewer than n are open, and ) while fewer have closed than opened. Every string finished is balanced.
How do you list the combinations of a list that add up to a target, each once, when the list has repeats?
Sort it, then choose from a place onwards: stop at a number larger than what's left, and skip a number equal to the one before it for the same choice.
How do you count the ways to place n queens, none attacking another?
One queen a row: keep sets of the columns, row + column and row - column taken, place a queen, count the rows below, and take it back.
How do you tell whether a word can be traced through a grid of letters, each cell used once?
First check the grid has enough of each letter. Then search from each cell: mark it, try its four neighbours for the next letter, and unmark it on the way back.
How do you list every way to split a text into palindromes?
Try each palindrome that starts at the current place, go on from its end, and take it back: reaching the end of the text gives a split.
Solving a sudoku by search, which empty cell should you fill first?
The one with the fewest digits left: one left is forced, and none left ends the branch at once, so the guesses go where there are fewest to make.
Joining ropes costs their lengths together each time. Which two should be joined first, and with what?
The two shortest, taken from a heap, with the joined rope pushed back: a rope's length is paid in every join it's part of, so the longest go through the fewest.
How do two heaps give the median of a stream after each number?
A max-heap holds the smaller half and a min-heap the larger, their sizes at most one apart, so the median is a top or the mean of the two.
How do you merge many sorted streams, some endless, in order?
Keep each stream's next item on a heap with the stream's place, give the smallest, and replace it with its stream's next: one item per stream is read ahead, never more.
How do you keep the kth largest of a stream as numbers come?
Keep only the k largest, in a min-heap: its top is the kth largest, and a new number replaces the top only if it's larger.
How do you order a text so no two alike letters touch, and when is it impossible?
Place the letter with the most left, from a max-heap, holding back the one just placed. It's impossible when one letter has more than half the letters, rounded up.
How do you find the k pairs from two sorted lists with the smallest sums?
Treat the pairs as a grid whose rows grow: a heap holds each of the first k rows' next pair, and taking one adds the pair after it in its row.
How do you insert an interval into a sorted list of intervals that don't overlap?
Copy those that end before it starts, merge in those that start before it ends, widening its ends, then add it and the rest. One pass, no sort.
How do you find where two sorted lists of intervals overlap?
Walk both: add the overlap of the two current intervals, from the later start to the earlier end, then move on from the one that ends first.
How do you remove the fewest meetings so that none of the rest overlap?
Keep the most: sort by end, keep the first, then each that starts once the last kept one has ended. The answer is the rest.
How do you refuse double bookings in a calendar, quickly?
Keep the bookings sorted by start, find the new one's place with bisect, and check only its neighbours: the one before must end by its start, the one after start by its end.
How do you find the times when no one in a group is busy?
Merge everyone's sorted busy times with heapq.merge and keep the latest end so far: a busy time that starts after it leaves a free time.
How do you find, for each query, the smallest interval that holds it?
Take the queries in sorted order: push intervals onto a heap by size as their left ends pass, pop those whose right end is behind, and read the top.
How long do tasks take on one machine when each task needs a cooldown between its runs?
The larger of the number of tasks and (most - 1) * (cooldown + 1) plus the count of tasks tied for the most, where most is the commonest task's runs.
How do you list the addresses a string of digits can be?
Choose each of four numbers' digits, one to three, stopping at a leading zero or a number over 255, and keep those whose fourth number ends at the last digit.
How do you cut a text into the most parts so that each letter is in only one part?
Find each letter's last place, then walk the text keeping the furthest last place so far: a part ends where the walk reaches it.
In the table where each 0 is written as 01 and each 1 as 10 in the next row, how do you find a row's symbol at a place without writing the row?
Climb a row: the place's parent is at (place + 1) // 2, and an even place flips the parent's symbol. Row 1's symbol is 0.
How do you find F(n) mod m when n is far too large to count up to?
Halve n: the pair F(k), F(k + 1) gives F(2k) and F(2k + 1), and the pair for 2k + 1 is one step on, with every number taken mod m.
How do you find the last person left when every kth person leaves a circle of n?
Counting from 0, J(1) = 0 and J(n) = (J(n - 1) + k) mod n, worked out in a loop from 2 up to n: as a recursion it goes a call deep per person.
How do you evaluate a prefix expression such as (+ 1 (* 2 3))?
Split it into parts, then read one expression with a function that reads a number, or an operator and then expressions up to the ), calling itself for each.
How do you count a list's inversions in n log n time?
Merge sort: while merging the sorted halves, a number taken from the right half crosses every number still waiting in the left half.
How do you find the closest pair of n points in n log n time?
Split by x and solve each half. Only points within d of the split, sorted by y, can be closer, and each needs checking against the next 7.
Which context does the JDK's HttpServer give a request to?
The one whose path is the longest prefix of the request's path, so a context at /tickets also takes /ticketsX and /tickets/T-1.
What does the length passed to sendResponseHeaders(status, length) mean?
Above zero, the body's exact length in bytes; zero, a body of any length, sent in chunks; -1, no body at all.
What must happen to an HttpExchange once its reply is written?
It must be closed, which ends the exchange. try (exchange) closes it however the handler ends.
What must a 405 response carry?
An Allow header listing the methods the address does take, as RFC 9110 requires.
What does Jackson 3 do by default with a field in the JSON that the record doesn't have?
It leaves it out. FAIL_ON_UNKNOWN_PROPERTIES was on by default in Jackson 2 and is off from 3.0, so an API that refuses unknown fields turns it on.
What does a fair Semaphore, new Semaphore(n, true), promise that one without fairness doesn't?
That waiting threads get permits in the order they asked. Without it, a thread that arrives as a permit frees can barge ahead of one that's been waiting.
What does Semaphore.tryAcquire() do when no permit is free?
It returns false at once, without waiting. It ignores fairness, taking any free permit even when other threads are waiting.
What happens to a thread interrupted while it waits in Semaphore.acquire()?
acquire() throws InterruptedException, clears the thread's interrupted status, and takes no permit.
What form does a Retry-After header's delay take?
A whole number of seconds, such as Retry-After: 120, or an HTTP date.
How does a handler on the JDK's HTTP server send a body whose length it doesn't know yet?
With sendResponseHeaders(status, 0), which sends the body in chunks until the stream is closed. flush() sends what's been written so far.
What ends one server-sent event?
A blank line, after its event: and data: lines. The stream is text/event-stream, in UTF-8.
When does a switch over a sealed interface need no default?
When its cases cover every class the interface permits. Then a new permitted class makes the switch fail to compile until it has a case.
Why measure a time limit with System.nanoTime() rather than the time of day?
It measures elapsed time only, so it doesn't jump when the computer's clock is set.
Which thread runs an HttpServer's handlers when it's given no executor?
The one thread start() made, so one exchange runs at a time. A virtual thread per exchange, from Executors.newVirtualThreadPerTaskExecutor(), lets each run on its own.
Why does a Filter that reads the request's body break the handler after it?
getRequestBody() gives the same stream every time, so once the filter has read it, the handler reads nothing.
Which path should an API's own handler take on the JDK's HttpServer, so that every address answers with its errors?
/, which every path starts with. A path no context takes gets the server's own 404, a page of HTML.
What does HttpServer.stop(seconds) do with the exchanges in flight?
It stops taking new ones, waits up to seconds for those in flight to finish, then closes their connections. Their handlers aren't stopped.
What does ExecutorService.shutdownNow() do to the tasks that are running?
It interrupts them, as a rule, and returns without waiting. awaitTermination waits for them to end.
Why name every wrong setting at once, rather than throw at the first?
Whoever deploys the service fixes them all in one go, rather than one deploy for each mistake.
What map does Collectors.groupingBy(Asked::service, Collectors.counting()) collect into, and how do you choose another?
A HashMap, in no promised order. The three-argument form takes a map factory, such as () -> new EnumMap<>(Service.class) or TreeMap::new.
comparing(PageCount::questions).thenComparing(PageCount::page).reversed(): what order does it give?
Most questions first, but ties by name backwards: reversed() turns around the whole chain before it. Reverse the count first, then add the tie-breaker.
Does list.stream().sorted() change the list?
No. A stream operation produces a result and doesn't modify its source.
Why does byHour take a ZoneId such as America/New_York rather than an offset such as -05:00?
A region's offset changes with the date, as when the clocks go back. A fixed offset would put every question on one side of the change an hour out.
What does \w match in a Java Pattern without any flags?
Only [a-zA-Z_0-9]. Pattern.UNICODE_CHARACTER_CLASS makes it match word characters in any script, as Python's \w does.
Why does the index lowercase with toLowerCase(Locale.ROOT)?
toLowerCase() uses the computer's language: in a Turkish locale, TITLE becomes tıtle with a dotless i, and a query for title would miss it.
What does map.computeIfAbsent(word, key -> new TreeSet<>()) give?
The word's set: the one in the map, or a new one it puts there first, like a defaultdict(set) in Python.
What does counts.merge(id, 1, Integer::sum) do?
Puts 1 for an id that isn't there, and otherwise adds 1 to its count.
How does a LinkedHashMap become an LRU cache?
Made with access order, new LinkedHashMap<>(16, 0.75f, true), each use moves an entry to the end, and an overridden removeEldestEntry returning size() > capacity drops the front entry after each put.
When can the JVM's garbage collector free an object?
When no reference from any live object reaches it. Anything a map still holds stays alive, so an unbounded cache fills the heap.
How big is a JVM's heap if nothing sets it?
At most a quarter of the machine's physical memory, by default. -Xmx sets the most.
What happens when a for-each loop over an ArrayList calls list.remove(element)?
The iterator is fail-fast, so its next step throws a ConcurrentModificationException, except when the loop happens to be at its end. list.removeIf(...) removes safely.
When does Collectors.toMap(keyMapper, valueMapper) throw?
When two elements map to the same key: an IllegalStateException. A third argument merges the two values instead.
Why can Integer counts compare equal with == in a test and not in production?
Java keeps one Integer object for each value from -128 to 127, so small counts are the same object. Larger ones are objects of their own, and == compares objects.
What goes wrong when code reuses one list, clearing and refilling it, as a HashMap key?
The map keeps that list object as the key of every entry, so changing it changes them all. Each key needs an object of its own that won't change, such as List.of(...) or a record.
Why is a HashMap of counts wrong when several Java threads add to it?
Java's threads run at once, and HashMap isn't synchronized: two threads can read the same count and write the same new one, losing an addition, or break the map adding keys.
What's the pattern for counting in a map from many threads?
A ConcurrentHashMap of LongAdders: counts.computeIfAbsent(key, k -> new LongAdder()).increment();, where computeIfAbsent runs atomically.
Can iterating a ConcurrentHashMap while other threads change it throw a ConcurrentModificationException?
No. Its iterators show the map at some point at or since they were made, and never throw it.
What are Java's virtual threads for?
A thread per task, such as per request, by the thousand: they're lightweight, and run on a few carrier threads, one per core.
In a record's compact constructor, summary = summary.strip(); changes what?
The constructor's parameter. Once the body ends, the compiler sets each field from its parameter, so the field gets the stripped summary.
When does Java's Ticket throw a NullPointerException, and when an IllegalArgumentException?
A NullPointerException, from Objects.requireNonNull, for a part that's null, and an IllegalArgumentException for a part that's there but wrong, such as an id that isn't T- and a number.
Why does Jackson write a record with a method isOpen() as JSON with a field open?
Jackson finds getters by their names, records included, and isOpen() is one. @JsonIgnore on the method leaves it out.
A record's compact constructor throws while Jackson reads JSON into it. What does the caller get?
A DatabindException, a ValueInstantiationException, whose cause is the exception the constructor threw.
What does calling add on a list from List.copyOf do?
It throws UnsupportedOperationException: the list is unmodifiable, so no caller can change what a class gives out with it.
Which exceptions are checked in Java?
Throwable and its subclasses, other than RuntimeException, Error and their subclasses. A method that can throw one has to catch it or name it in its throws clause.
When should a Java method throw a checked exception rather than an unchecked one?
When its caller can reasonably be expected to recover, such as a callback time that's taken. A mistake in the calling code, such as a blank summary, is unchecked.
Why does callbacks.remove(new Callback(ticket, day, time)) find the callback booked earlier?
remove removes an element equal to the one it's given, and a record's equals compares each of its parts.
Why can't a lambda that calls a method throwing a checked exception be a java.util.function.Function?
Function's apply declares no exceptions, so the lambda would have to catch it. A functional interface of your own can declare throws, as Action does.
Why does Tool take its arguments' record as a Class<A>, rather than using A.class?
Type parameters are erased: at run time A is just its bound, so A.class doesn't compile. The Class<A> value carries the type for Jackson and reflection.
How does a method with a Tool<?> call that tool's action with the record it read?
Through a private generic helper, <A extends Record> String call(Tool<A> tool, ...), which captures the wildcard as A.
A try has catch (DatabindException e) and then catch (ValueInstantiationException e). What's wrong?
ValueInstantiationException is a DatabindException, so the first catch takes it. The more specific catch goes first; here, javac refuses the code.
Why can id == "T-2041" pass a test and fail in production?
== compares references. Java interns string literals, so two literals are one object, but an id read from JSON or built at run time is a new object with the same text. equals compares the text.
List<Integer> slots, int slot: which method does slots.remove(slot) call?
remove(int index), which removes the element at that place. Java tries overloads without boxing first. slots.remove(Integer.valueOf(slot)) removes the value.
In what order does a HashMap give its keys?
In no promised order: whatever its hash codes give. A TreeMap sorts its keys, and a LinkedHashMap keeps the order they were added in, as Python's dict does.
What title does a problem details object of type about:blank and status 422 have?
Unprocessable Content, the phrase RFC 9110 gives 422. Its title should be the status's own phrase, and RFC 9110 also renamed 413 Content Too Large.
Why does the desk's 500 problem have no detail?
The detail is for the caller to correct the request. An exception's message can hold connection strings, paths or queries, which belong in the server's logs.
How is the JSON key a/b~c written in a JSON Pointer?
/a~1b~0c: ~ becomes ~0 first, then / becomes ~1. Escaping / first would turn its ~1 into ~01.
A switch over an exception has case DatabindException e before case InvalidDefinitionException e. What happens?
It doesn't compile: the first label dominates the second, since every InvalidDefinitionException is a DatabindException.
What is k-fold cross-validation's estimate of a model's error?
The mean of k errors: the data is dealt into k folds, and each fold's error is measured on its own rows by a model fit on the other folds.
Which preprocessing steps must be fit inside each fold of cross-validation?
Any that learn from the data, such as a standardization's means and spreads. A step with nothing to learn, such as products of the measurements, can be made once for every row.
What does the number of folds trade off in k-fold cross-validation?
More folds fit each model on more of the data, so its error is closer to that of a model fit on all of it, but the models overlap more, so the estimate varies more from one shuffle to another. Five and ten folds are the usual choices.
Why does an error measured on one validation split vary from split to split?
It depends on which rows the split happens to hold out, and with few rows a handful of hard ones can move it a long way.
How does k-fold cross-validation choose between models?
It scores every candidate on the same folds and keeps the one with the lowest mean error over them.
Why are all the candidates scored on the same folds?
So that a difference between their errors comes from the models, not from one of them drawing easier folds.
Why does the best cross-validated error of many candidates read too low?
Each candidate's error carries some luck, and the lowest is likely to be one whose luck ran good, so it reads lower than that candidate's error on new data.
What does nested cross-validation measure?
The error of the whole procedure, the choice included: each outer fold makes the choice afresh on its own fit rows and scores it on rows the choice never saw.
What does nested cross-validation cost, with k outer folds?
k times the fits of one cross-validation, plus a fit of each outer fold's choice: every outer fold repeats the whole search.
Why must the test set stay out of tuning?
Its error is the estimate for new data only if nothing was chosen or fit on it. Tuned on it, the model has seen those rows, and the test error reads too low.
What is data leakage?
Information the model wouldn't have when predicting, such as held-out rows or the test set, reaching it while it is built, so its measured error reads better than it is.
Why does standardizing on every row before cross-validation leak?
Each fold's scaling then comes partly from its held-out rows, so they help shape the model that predicts them.
What is repeated cross-validation?
k-fold cross-validation run with several shuffles of the folds, each candidate's error averaged over them, so the choice doesn't turn on how one shuffle fell.
What can't repeating cross-validation fix?
The luck of which data was collected: every shuffle deals the same rows, so it steadies only what depends on the shuffle.
What does repeating cross-validation cost?
A whole cross-validation for each shuffle: two shuffles fit twice the models of one.
What does a weight in a linear regression mean?
How much the forecast changes when its measurement rises by one unit and the others stay as they are.
Why does the mean squared error square each miss?
So that a miss in either direction counts against the model, and one large miss counts far more than several small ones.
What is the simplest forecast a regression model has to beat?
The mean of the training outputs, given for every sample whatever its measurements.
What are the normal equations of least squares?
, where holds the training measurements with a column of ones, and the weights, then the bias.
Why solve least squares with np.linalg.lstsq rather than by inverting ?
Forming squares the condition number, so the inverse loses digits to rounding. lstsq works from itself, and when one column repeats another, it still returns one answer: the shortest.
What is the gradient of the mean squared error with respect to the weights?
holds the samples, one row each, and the misses.
How large can gradient descent's learning rate be on a mean squared error?
Less than , where is the error's largest curvature. Past it, each step lands further from the minimum than the last, and the loss grows without end.
Why does standardizing the measurements speed up gradient descent?
It lowers the condition number. Unscaled, a column with a large mean and a small spread, such as air pressure, pulls against the bias.
In what order must a model's measurements come when it forecasts?
In the order it was fitted with. In any other order, every weight meets the wrong measurement, and nothing raises an error.
How do you make a table with one row per sample from columns that may be single numbers?
np.column_stack(columns). np.array(columns).T gives a flat row when each column is a single number.
What should a forecaster do with an hour that has no weather?
Raise an error that names the hour. Dropping it moves every later forecast onto the wrong hour.
Why not clip forecasts to the range of the outputs in the history?
An unusual hour then gets a number that looks right, and nobody learns that the model forecast something else.
Why are the misses for a forecast interval measured on hours the model wasn't fitted to?
On the hours a model was fitted to, its misses tend to be smaller, so an interval built from them would be too narrow.
From held-back misses, which one sets the interval's half-width for a coverage of ?
The miss size at rank , counting from the smallest. When is more than , the interval has no end.
What does an interval's coverage promise?
That over hours like the held-back ones, at least that share of the outputs fall inside their intervals, on average. It isn't promised for every kind of hour.
How does logistic regression turn a sample's measurements into a probability?
It weights and adds them into a score, , and takes the score's sigmoid, .
Why can't sigmoid compute for every score?
For a large negative score, is too large for a float. For negative scores, use the same number in another form, , where is between 0 and 1.
What does cross-entropy cost a sample with label 1 that was given a probability ?
: 0 for a sure, right answer, growing without limit as a wrong answer becomes sure.
In logistic regression, what is the derivative of a sample's cross-entropy with respect to its score?
The error, . The sigmoid's slope, , cancels the denominator of .
How do you check a gradient you worked out?
Against central differences: move one weight by a small each way, and divide the change in the loss by .
Why standardize the columns before training by gradient descent?
Columns on very different scales need very different learning rates: one small enough for the largest column is far too small for the smallest. Standardized, every column has mean 0 and standard deviation 1.
Whose means and standard deviations scale the test set?
The training set's. A model meets new samples after it is trained, often one at a time, so their own numbers aren't known.
What goes wrong when gradient descent's learning rate is too large?
A step jumps past the minimum, so the loss goes up instead of down.
In what order are the columns of scikit-learn's predict_proba?
In the order of model.classes_. With labels 0 and 1, column 1 is the probability of class 1.
What goes wrong when a model scores new samples after fit_transform?
The scaler learns new means and standard deviations from every batch: training–serving skew. A sample scored on its own becomes all zeros. transform reuses the numbers learned from the training set.
What does class_weight="balanced" do to a logistic regression?
It counts the rarer class's samples more heavily in training, so it fits a different model, with different probabilities.
What is precision?
The share of the samples predicted positive that are positive: .
What is recall?
The share of the positive samples that are predicted positive: .
Which threshold reaches a recall of with the fewest samples predicted positive?
Sort the positive samples' probabilities from highest to lowest, and take the one at position , where is how many positive samples there are.
What is the difference between np.sort(p) and p.sort()?
np.sort(p) returns a sorted copy. p.sort() sorts p itself, so the caller's array changes.
What does a point on a ROC curve show?
At one threshold, the share of positives flagged, the true positive rate, against the share of negatives flagged, the false positive rate.
What chance is the ROC AUC?
The chance that a randomly chosen positive scores higher than a randomly chosen negative, with a tie counting half.
Why doesn't a ROC curve change when positives become rarer?
Each of its rates counts within one class, so changing how many of each class there are leaves both rates as they were.
Why does accuracy say little when one class is rare?
Always predicting the common class scores that class's share, 90.8% of the pulsar candidates here, without finding a single positive.
What are precision and recall?
Precision is the share of flagged items that are positive. Recall is the share of positives that are flagged.
At the same threshold, why does precision fall when positives become rarer?
The true and false positive rates stay the same, but each positive comes with more negatives, so the false alarms grow against the positives found.
About what average precision do scores in a random order get?
About the share of positives, since their precision at any recall is about that share.
How does average precision sum up a precision–recall curve?
Each threshold's precision, weighted by the recall it adds over the next higher threshold.
What makes a classifier's probabilities calibrated?
They come true as often as they say: of the items given about 0.8, about 80% are positive.
With calibrated probabilities, how many positives should you expect among some items?
The sum of their probabilities.
Why doesn't a lower Brier score by itself show better calibration?
The Brier score rewards a sharp ranking as well as calibration, so a model can score lower with probabilities that run too high or too low.
What does weighting the positive class in training do to a model's probabilities?
It fits the model to data where positives are that much more common, so its probabilities run high, while its ranking barely changes.
Why does a ROC AUC computed from a classifier's yes-or-no answers come out lower?
Answers of 0 and 1 have one threshold, so the curve has one corner, and its area is the mean of the true positive and true negative rates there.
Why can't precision measured on a balanced sample be used for a rare class?
Precision depends on the share of positives: a sample with more of them than the real data reads higher than the real data will.
Why is counting the items at or above 0.5 the wrong way to estimate how many positives there are?
Items below the threshold still hold positives, and those above it aren't all positive. With calibrated probabilities, the expected count is their sum.
How do you move a classifier's probabilities to a new share of positives?
Multiply each probability's odds by the new share's odds over the odds of the share it was fit on, then turn the odds back into a probability.
What does moving probabilities to a new share of positives leave unchanged?
The order of the items, so the ROC curve and the list of the most likely ones stay the same.
What does adjusting probabilities to a new share of positives assume?
That each class looks as it did in training, and only how common the classes are has changed.
What are the polynomial features of degree 2 of some measurements?
The measurements themselves, and every product of two of them, each measurement times itself included.
Why is a model on polynomial features still fit by least squares?
It is still linear in its weights: the products are only more columns in the table it is fit to.
How many polynomial features do measurements make, up to degree ?
: every product of up to of them, without the column of ones.
Why does least squares' training error fall at every degree?
Each degree's features include the last degree's, so it can always do at least as well on the training data, by giving the new features no weight.
What is overfitting?
A model fitting its training data much better than it predicts new data: its extra flexibility fits the noise in the training data.
Why are the concrete tests split by mix, not one by one?
A mix is tested at several ages. Split one by one, a held-out test's mix is often among the training tests, and the error measures memory, not prediction.
What are the training, validation and test sets each for?
The training set fits the weights, the validation set chooses between models, and the test set, used once at the end, gives the honest error on new data.
What does ridge regression add to least squares?
A penalty on large weights: it minimizes the mean squared error plus , with the bias left out of the penalty.
Why are the features standardized before ridge regression?
The penalty counts every weight alike, so a feature on a large scale, whose weight is small whatever its effect, would barely be penalized.
Which alpha in scikit-learn's Ridge matches a penalty on the mean squared error, with training rows?
: Ridge penalizes the sum of the squared errors, not their mean.
Why does choosing a model by its training error go wrong?
The training error always favors the most flexible model, which fits the training data's noise. A choice needs data the fit never saw, such as a validation set.
When the data has several rows per unit, such as tests of one mix, how should rows be held out?
By whole units, with every row of a unit on the same side, as GroupShuffleSplit does. Otherwise the error measures memory of the units, not prediction for new ones.
Why does a mistake that leaks training data into scoring often go unnoticed?
Nothing fails: the code still returns numbers, and they look better than they should, so the mistake hides behind a good result.
Why should a model refuse an input outside the ranges of its training data?
Nothing it was fit on shows how it behaves there, and a flexible model can miss there by far more than the error measured inside the ranges.
Why is checking each input against its training range a first guard, not a complete one?
The ranges are taken one input at a time, so an input can sit inside every range and still combine them in a way no training example comes near.
Why does the validation error of the chosen model tend to understate its error on new data?
It is the lowest of the errors of every model tried, and a model can come out lowest in part by suiting the validation data. The error to report comes from a test set used once, after the choice.
How do you read a binary tree level by level without a queue?
Keep one level's nodes in a list; the next level is their children, in order. Stop when a level is empty.
How do you find a binary tree's diameter in one walk?
Work out each node's height from its children's, bottom up; the longest path through a node as its highest has the two children's heights added, in edges. Keep the largest.
In one walk, how does each call find the lowest common ancestor of nodes a and b?
A call returns None if its subtree holds neither, itself if it is a or b, itself if both children found one, and otherwise whatever one child found.
Why does a preorder list of a tree's values need a mark for each missing child to rebuild it?
The marks say where each subtree ends, so reading back in the same order puts every node in its place; without them, different shapes give the same values.
Counting a tree's downward paths that add up to a target, what do you look up at each node?
How many running totals on the way from the root equal the node's own total minus the target: each starts a path that ends here.
For a tree's largest path sum, what does each node give its parent, and what does it check?
It gives its value plus its better child's best way down, taking nothing below 0, and it checks its value plus both children's best ways down against the largest so far.
How do you tell whether every room opens, starting in room 0 with the keys inside?
Search from room 0 with a list as a stack and a set of rooms opened, adding each key's room once. Every room opens if the set ends up holding them all.
How do you capture every region of Os that doesn't touch the board's edge?
Search from every O on the edge and mark what you reach: those regions are safe, and every O left unmarked is captured.
How do you find the squares of a height grid whose rain reaches two oceans?
Search uphill from each ocean's edge squares, to neighbours at least as high, and take the squares both searches reach.
How do you find a cycle among steps that need other steps first?
Walk depth first, keeping the steps on the current path and those finished. Reaching a step on the path closes a cycle; a finished step adds nothing.
How do you find the longest strictly rising path in a grid of heights?
The longest from a square is 1 more than its best higher neighbour's: work the squares out from highest to lowest, or search depth first with a memo.
How do you answer many queries of x / y from equations a / b = value?
Search each connected group once, giving each variable its value over the group's root; a query in one group is the ratio of its two values.
How do you put tasks in an order that keeps every "this before that", and tell when no order can?
Count each task's unmet prerequisites. Take tasks whose count is 0, lowering the counts of the tasks that need them. If you run out before taking them all, some tasks need each other.
A sorted list of words in an unknown alphabet: what does each pair of neighbours tell you?
At most one rule: at the first place they differ, the first word's letter comes before the second's. A word followed by a shorter word it begins means no order sorts them.
Adding links one at a time, how do you tell quickly whether a link joins two nodes that are already connected?
Union-find: each group is a tree of links to a leader. Follow each end to its leader; the same leader means already connected, else point one leader at the other.
Accounts that share an email address belong to one person, even through other accounts. How do you group them?
Union-find over the addresses: join each account's addresses together, then gather the addresses by their group's leader.
In Dijkstra's algorithm, why is the soonest arrival in the heap final?
Every other way to that node leaves through a node reached no sooner, and no link takes less than 0, so it can only arrive later.
When finding the cheapest route with at most k stops, why does each round read the fares from a copy made at its start?
So each round adds at most one flight: a fare lowered in the same round would let two flights chain in one round, and round k would no longer mean at most k flights.
Why does a flood fill of a large grid keep a list of squares to visit rather than call itself on each neighbour?
Each recursive call goes one level deeper, and a long island goes deeper than Python's recursion limit. A list holds the same work as data, which can grow as far as memory allows.
Why does a breadth-first search find the shortest way through a grid, when a depth-first one may not?
It takes squares in the order they were reached, so it finishes every square n steps away before any n + 1 away: the first time it reaches a square is by a shortest way.
Several fires spread through a grid at once. How do you find the minute each square catches with one search?
A breadth-first search that starts with every fire in its first level: each level is the squares one more minute away from the nearest fire.
Copying a graph with cycles, how do you make each node's copy once and still link every copy to the right ones?
A dict from each original node to its copy: a link looks up the copy that may already exist, and the dict is also the search's record of the nodes seen.
How do you find the words one letter away from a word without comparing it with every other word?
File every word under each pattern it fits with one letter blanked out, such as c*ld for cold. Words a letter apart share a pattern.
How do you tell whether a graph's nodes can be split into two groups with every link between the groups?
Search from each node without a group, giving every node reached the group opposite to the one it was reached from. A link between two nodes of one group means no split exists.
Reversing a linked list in place, what must you save before you point a node's next back?
The node after it, so the rest of the list stays reachable once its next changes.
Once the slow and fast pointers meet in a list's cycle, how do you find where the cycle starts?
Start one pointer again from the head and move both one node at a time: they meet at the cycle's start, as many steps from the meeting point as from the head.
Why start with a node in front of the head when removing the nth node from a list's end?
Every node, the head included, then has a node before it to relink, so removing the head needs no branch of its own.
Adding two numbers held as digit lists, ones digit first: when does the loop stop?
When both lists have run out and there's no carry left, so a last carry becomes a digit of its own.
How do you reorder a list as first, last, second, second to last, and so on, in place?
Find the middle with slow and fast pointers, reverse the second half, then interleave the two halves.
Copying a list whose nodes have random links, how do you find the copy of the node a random link points to?
Make every copy first, kept in a dict by its original node, then set each copy's links from the dict.
What two structures make a least recently used cache's get and put take constant time?
A dict from each key to its node, and a doubly linked list of the nodes in the order they were used, so a node moves to the newest end or drops off the oldest in a step.
What does np.mean(X, axis=0) give for an array with a row per sample?
One mean per column: axis 0 runs down the rows, and the reduction collapses it.
What does scikit-learn's StandardScaler divide by in each standard deviation?
The number of rows, as np.std does by default. ddof=1 divides by one fewer.
Does indexing an array with a boolean mask give a view or a copy?
A copy: a mask is advanced indexing, which always copies. A slice is a view.
When can NumPy broadcast two shapes together?
Compared from the last axis back, each pair of lengths is equal or one of them is 1. An axis one shape lacks counts as 1.
A has the shape (m, d) and B the shape (n, d). What shape is A[:, None, :] - B[None, :, :]?
(m, n, d): every row of A against every row of B.
Why are new samples standardized with the records' mean and standard deviation?
So every sample is on the records' scale, and a sample that arrives alone can still be standardized: one row has no spread of its own.
Before standardizing, how much of the squared distance between the samples came from the two area columns?
99.2%, so the nearest records were little more than those with the nearest areas.
What does np.argsort(D, axis=1) return?
For each row of D, the column positions that would sort it, smallest first.
Why sort with kind="stable" when equal values are possible?
A stable sort keeps equal values in the order they came. The default sort promises no order for them.
What order does np.argpartition leave the k smallest values in?
No promised order: it only puts them before the rest, so they must be sorted after it.
near has the shape (m, k). What shape does diagnoses[near] have?
(m, k): indexing with an array of positions gives an array of their shape.
Why are new samples never standardized with their own batch's mean and standard deviation?
Their scale would then depend on the batch, so a sample's nearest records would too. Alone, a sample has no spread to divide by.
What does X -= mean do to an array the caller passed in?
It changes the caller's array: an in-place operator writes into it. X - mean makes a new array.
For a batch, what does diagnoses[near].mean() give, with no axis?
One number for the whole batch, the mean over every row. axis=1 gives one per row.
Why does looking up new samples in chunks find the same records as looking them up all at once?
Each sample's distances form its own row, so chunks change how many rows are worked on at once, not the answer.
How many bytes do the differences between r samples and n records of d measurements take, in 64-bit floats?
r × n × d × 8, and their squares as many again.
In a loop over chunks, why can memory hold two chunks' arrays at once?
The last chunk's arrays stay alive until their names are given the new ones, while the next chunk's are made.
Halve the step of a central difference. What happens to its truncation error?
It falls to about a quarter: it is in proportion to the square of the step. A forward difference's only halves.
Why does a finite difference get worse when its step is too small?
Each value of f is rounded to about machine epsilon times its size, and dividing that rounding by a tiny step makes it large.
About what step suits a central difference in float64?
The cube root of machine epsilon, , times |x| when |x| is above 1.
What is element i of a function's gradient?
The function's slope as element i of its input alone changes: its partial derivative along that element.
What is the gradient of the distance from z to a fixed point r?
: the vector of length 1 that points away from r.
Why compare two gradients by their relative error, not by the length of their difference?
The length of the difference is small for any small gradient, right or wrong. As a share of the longer gradient, it says how many digits agree at any scale.
What is the derivative of the mean log loss with respect to one sample's score?
: the sample's probability less its diagnosis, over the number of samples.
What is the derivative of tanh?
, so the backward pass can take it from the forward pass's outputs.
Why do numerical gradients check a gradient rather than train with one?
They take two evaluations of the loss for each parameter, where backpropagation gives the whole gradient for a few times the cost of one.
Why can a gradient check at zero weights pass a wrong gradient?
Many terms are 0 there whatever the code says, such as tanh's outputs and the weights the decay acts on, so mistakes in them don't show. Check at random weights.
What does weight decay of add to a weight's gradient?
: the weight times l2.
How do you work out for large scores without overflow?
np.logaddexp(0, s), which stays finite where np.exp(s) overflows.
What is the slope of f at x along a vector v, in terms of its gradient?
, the dot product of the gradient with v.
Why does checking a gradient along a random direction find a wrong gradient?
A wrong gradient g + e is off along v by e · v, which is 0 only when v is at a right angle to e, and a random direction almost never is.
Why measure a directional check's error against the gradient's length rather than the slope along the direction?
The slope along a random direction can be close to 0 even when the gradient isn't, and the gradient's length keeps the tolerance the same at every scale.
How do shuffled batches use each record exactly once in an epoch?
They cut one random order of all the row numbers, from rng.permutation(n), into consecutive batches.
What does a generator made with the same seed give?
The same numbers, every run, so a training run can be repeated exactly.
An epoch's batches have different sizes. How do their means combine into the mean of all the records?
Weighted by their sizes: together the batches hold every record once.
What is the expectation of a random batch's mean?
The mean of all the records, for any batch size: on its own, each draw is equally likely to be any record.
What is the variance of the mean of a batch of rows drawn without replacement from records whose values have variance ?
: the variance with replacement, , times a factor that is 1 for one row and 0 for every record.
A batch is made four times as large. How much less does its mean stray?
About half as much: its standard deviation falls like . Drawn without replacement, it falls a little further.
How does a uniform number pick a row with a probability in proportion to its weight?
Scaled by the weights' total, it lands in one of the stretches between running totals, each as long as a row's weight. The first running total above it names the row.
Why scale the uniform number by the weights' total, rather than trust weights that add up to 1?
They add up to 1 only to within rounding. A running total that ends a little under 1 lets the largest uniform numbers land past the last row.
A batch is drawn with probabilities . How does it estimate the mean over all records?
By counting each row's value as . The reweighted mean is right on average.
Why does a sampler that makes its generator from the seed alone give the same batches every epoch?
Its generator starts from the same state each epoch, so it draws the same order. Make it from the seed and the epoch's number together.
An epoch draws row numbers with replacement from records. About what share of the records does it leave out?
About : each record is missed by every draw with probability .
A batch's per-record losses are added up. What should the total be divided by?
The number of rows in that batch. Dividing by the size asked for shrinks the average of the last, shorter batch.
Why must every worker draw the epoch's order from the same seed?
So that they share one order and take turns through it. Orders drawn separately overlap, and leave records out.
Why does a generator seeded with seed + epoch repeat orders across runs?
Pairs that add up to the same number get the same stream: seed 1's first epoch is seed 0's second. A generator made from [seed, epoch] gives every pair its own.
How does PyTorch's DistributedSampler give every process the same number of records when the processes don't divide them?
It carries the order on from its start until they do, so a few records are used twice, unless it is told to drop the remainder instead.
How is a bootstrap resample of a sample of drawn?
As rows drawn from the sample with replacement, so some rows come more than once and others not at all.
What does the standard deviation of a bootstrap distribution estimate?
The statistic's standard error: how far it would vary over new samples of the same size.
A share is measured on samples. What standard error does the bootstrap come close to?
, the formula for a share.
What does a 95% confidence interval promise?
That the method covers the true value for 95% of the samples it could be given. One interval either covers it or doesn't.
Which quantiles of a bootstrap distribution bound a 95% percentile interval?
Its 2.5% and 97.5% quantiles, which leave 2.5% of the resamples outside on each side.
How does a basic interval use the bootstrap distribution's quantiles?
It reflects them about the estimate: from twice the estimate minus the high quantile to twice the estimate minus the low one.
What does BCa's bias correction measure?
How far the estimate sits from the middle of the bootstrap distribution: the standard normal quantile of the share of resamples below it, counting a tie as half.
For a mean, what is BCa's acceleration ?
The sample's skewness over : the jackknife's is minus the sample's mean.
When is a BCa interval the same as the percentile interval?
When : the estimate sits in the middle of the bootstrap distribution, and the jackknife shows no skew.
Why does resampling without replacement give a bootstrap interval of no width?
Each resample holds every sample once, so a statistic such as the accuracy comes out the same on every one.
A model's predictions are resampled to bootstrap its accuracy. What must be resampled with them?
The labels of the same samples, with the same row numbers, so that each prediction still meets its own sample's label.
At what quantile levels does a percentile interval at confidence take its ends?
At and , leaving outside on each side.
Why resample two models' results together to compare their accuracies?
The samples they agree on move both accuracies together and cancel, so the interval for the difference is narrower than resampling each model's results apart.
On a resample, what decides the difference between two models' accuracies?
Only how many of its samples each model alone gets right: .
How can a paired bootstrap of two models' accuracies skip drawing every sample?
By drawing how many samples land in each group, a alone right, b alone right and the rest, with rng.multinomial: three numbers a resample.
What does the dot product of two vectors equal, in terms of their lengths?
The product of their lengths and the cosine of the angle between them.
What does a cosine of 0 between two vectors say?
They are at a right angle: neither points any way along the other.
Why do the two class means of standardized records point in opposite directions?
Standardized, the records average 0, so the class means, each weighted by its count, add up to 0.
When is a sample nearer the malignant mean than the benign one?
When its position along the direction from the benign mean to the malignant one, measured from their midpoint, is above 0.
Z has the shape (n, d) and u has d numbers. What does Z @ u give?
n numbers: each row's dot product with u.
What shape does the boundary of the nearest-centroid rule between two classes have?
A flat hyperplane, halfway between the two means and at a right angle to the line joining them.
A has the shape (m, d) and B the shape (n, d). What does A @ B.T hold?
The dot product of each row of A with each row of B, in an array of shape (m, n).
How does a squared distance come from dot products?
Why can distances worked out from dot products go wrong for samples far from 0?
Their squared lengths are large and nearly equal, so subtracting them leaves mostly rounding: catastrophic cancellation.
Why doesn't ranking records by their dot product with a sample find the nearest ones?
A dot product grows with both lengths, so it favours records far from the average in the sample's direction. Nearness takes the distance.
Scores taken along mu1 - mu0, not along a vector of length 1: what changes?
Every score is multiplied by the length of mu1 - mu0. The signs stay, but the scores are no longer in standard deviations.
Why isn't the records' mean the midpoint of the two class means?
It weights each class's mean by its count, so it sits nearer the larger class's mean.
Why move every sample by the records' mean before working out distances from dot products?
It changes no distance, and it keeps the squared lengths, which are added and subtracted, the size of the records' spread.
Worked out from dot products, why can a sample's distance to its own copy come out above 0?
is 0 only if every term rounds alike. A few units in the last place off, its square root is far larger.
How do you get exact distances to a sample's k nearest records without an array of every difference?
Find the k with the dot products, then work out their distances again from their differences.
What is the Gini impurity of a group of calls with a share of yes?
: 0 when every call said the same, and 0.5 for an even mix.
What impurity does a split leave?
The two sides' impurities, each weighted by its share of the calls.
Why does a decision tree need no scaling of its columns?
A split compares a column with a threshold, so only the order of the column's values counts, and scaling keeps that order.
How does a decision tree score a new call?
It follows the call's answers from the root to a leaf, and gives the share of yes among the training calls in that leaf.
How does a category such as a job become columns a tree can split?
One column for each category, 1 for the calls in it and 0 for the rest. A split at 0.5 asks whether a call is in that category.
Why is a decision tree called greedy?
Each node takes the best split for its own calls, without looking ahead to the splits below it.
Why does a tree grown deep hold every shallower tree?
Each node's split depends only on its own calls, not on how deep the tree may grow below it, so cutting the deep tree at a depth gives the tree grown to that depth.
As a tree grows deeper, what happens to its AUC on its training data and on validation data?
The training AUC keeps rising. The validation AUC rises, peaks and falls, as the deeper splits fit the training data's chance patterns.
Which two limits keep a decision tree from fitting its training data too closely?
Its depth, and the fewest training rows a leaf may hold.
Why can't the length of a call rank the clients to call?
It is known only once the call ends, so clients not yet called have none.
Why can't a tree's AUC on its training data choose its depth?
It never falls as the tree deepens, so it favours the deepest tree, however that tree does on new data.
What should one-hot encoding give a category the training data never had?
0 in every column of its category column, rather than an error.
How does a decision tree become a list of rules?
One rule for each leaf: the conditions on the path from the root to it, each column's splits merged into one condition, with the leaf's counts.
Why write a rule's category condition as the categories it rules out?
It then holds for every other category, including one the training data never had, just as the tree sends that category down the same path.
How can anyone check a rule's share of yes without the model?
Count the training rows that meet its conditions, and those of them that said yes.
What does each tree of a gradient-boosted model fit?
The residuals the trees before it left: what each training row said, less the share the model gives it so far.
What value does a boosted tree's leaf take, for the log loss?
over its rows: one Newton step in the log-odds towards their least log loss.
Where does the learning rate enter a boosted model?
Each tree's values are multiplied by it before they're added to the log-odds, so each tree moves the scores only part of the way.
Why does a boosted model's validation loss turn up while its training loss goes on falling?
Each tree is fitted to the training rows, their noise included; past some round, the trees fit noise that new rows don't share.
How does early stopping with a patience of 20 choose the rounds?
It stops once 20 rounds in a row pass without a validation loss lower than the least so far, and keeps the rounds up to that least.
What does a smaller learning rate change in boosting?
Each tree moves the scores less, so the validation loss takes more rounds to reach its least, which tends to be lower.
What weight does XGBoost give a leaf?
: the sum of its rows' gradients over the sum of their hessians plus lambda, with the sign turned.
Why does XGBoost's lambda shrink a light leaf's weight more than a heavy one's?
It's added to the leaf's sum of hessians, which is small for a light leaf, so it changes that leaf's ratio much more.
What does XGBoost's min_child_weight count?
The sum of the hessians in each part of a split, so a row whose share is near 0 or 1 counts for little.
When eval_set holds more than one set, which does XGBoost's early stopping watch?
The last. With the training rows last, it watches their loss, which keeps falling, and never stops early.
Why not code each set of rows with its own pd.factorize?
pd.factorize numbers categories in the order they first appear, so each set gets codes of its own, and a code means one category in the training rows and another in new ones.
What does scale_pos_weight above 1 do to XGBoost's chances?
It moves them up, so they add up to more positives than there are. Leave it at 1 when the chances are used as chances.
What does each entry of LightGBM's monotone_constraints mean?
There's one for each column, in order: 1 for a prediction that never falls as the column rises, -1 for one that never rises, and 0 for no constraint.
How does LightGBM code a pandas category column in rows it scores after training?
By the training rows' categories, which it keeps with the model, and a category they never had as missing.
Why can a boosted model move a client up a list for being called again?
Its trees follow the ups and downs of the calls they learn from, such as clients called three times saying yes more often than those called twice. A monotone constraint rules it out.
What is the first principal component of a set of rows?
The direction of length 1 along which their scores vary most: the eigenvector of their covariance matrix with the largest eigenvalue, which is that variance.
In what order does np.linalg.eigh give eigenvalues, and where are the eigenvectors?
From the smallest up, with the eigenvectors as the columns of its second array, in the same order.
Why scale measurements before finding their principal components?
Unscaled, the measurement with the widest spread takes the first component almost alone. Among the beans, the areas in pixels take almost all the variance.
What are the two steps k-means repeats?
Each row joins the group of its nearest centre, then each centre moves to the mean of its group's rows, until no row changes group.
Why can't a round of k-means raise the inertia?
Joining its nearest centre can only shorten a row's distance, and a group's mean is the point with the least sum of squared distances to its rows.
Why might the groups k-means finds not match the classes in the data?
It groups rows by distance alone: a class spread wide can take two groups, and one that lies among others can take none.
How does k-means++ choose its starting centres?
The first is a row drawn at random, and each next one a row drawn with a chance in proportion to its squared distance to the nearest centre so far.
What does a row's silhouette compare?
Its mean distance to the other rows of its group, a, with its mean distance to the rows of the nearest other group, b: (b − a) / max(a, b).
Why run k-means from several starts?
Each start can end in other groups. Keeping the run with the least inertia guards against an unlucky start.
Why not run k-means afresh on each new batch to group it?
A new run finds groups of its own and numbers them afresh, so its group 3 needn't be the training beans' group 3. New rows join the training groups, by their nearest centre.
Why scale measurements before k-means?
k-means groups by distance, and unscaled the widest measurement decides it. Among the beans, area in pixels splits the largest variety into several groups by size.
How is a group of a clustering named from labelled training rows?
By the commonest label among its own training rows, counted within the group.
How can groups found without labels estimate a batch's make-up?
Name each group by the commonest label among its training rows, name each new row by its group, and count the names.
Why choose enough groups for every class to name one?
A class that names no group is never given to a row, so its share always comes out 0.
Why can a make-up be closer than the names it counts?
Rows of one class named another are partly balanced by rows of that class named the first, so the counts err less than the names.
What number does target encoding give a category?
The share of its rows with the outcome, pulled toward the share of all rows by a smoothing weight m: (y + m·p) / (n + m).
Why are a model's fit rows cross-fitted when they are target encoded?
So that no row's encoding is learnt from its own answer: each is learnt from the rows outside its fold.
Does more smoothing stop a target encoding from leaking a row's answer?
No. It shrinks the answer's pull, 1 / (n + m), but only learning the encoding from other rows removes it.
Why score every model on the same folds?
A fold that is hard for one model is hard for the others, so on the same folds its difficulty cancels out of the difference between them.
What does scikit-learn's clone return?
A new, unfitted model with the same settings.
What does repeated k-fold cross-validation repeat?
The k-fold split, each time with a new shuffle of the rows, so that every model is scored on more folds.
Why is the usual standard error too small for differences over repeated cross-validation folds?
The folds' fit sets share most of their rows, so the models are alike and the differences move together rather than independently.
What is Nadeau and Bengio's corrected standard error of a mean difference over n folds?
s · √(1/n + n_held / n_fit), where s is the differences' standard deviation.
Do more repeats of cross-validation shrink the corrected standard error toward 0?
No. Its n_held / n_fit term stays as folds are added, so it levels off.
How can a target encoding leak into cross-validation?
Learnt on every row before the folds, each held-out row's encoding takes in its own answer, so the scores come out higher than on new rows.
What does a shuffled k-fold splitter need for every model to get the same folds?
A fixed random_state. Without one, each call to split shuffles anew.
Which quantile of the t distribution ends a two-sided 95% interval?
The one at 0.975, which leaves 2.5% beyond each end. The one at 0.95 ends a 90% interval.
With m candidates compared with a baseline, at what confidence is each interval set for all of them to hold together at confidence c?
1 − (1 − c) / m, Bonferroni's correction.
When does a candidate beat the baseline in the bank's review?
When its whole interval for the difference, at the shared confidence, lies above 0.
Why score a model on the latest calls as well as on shuffled folds?
Shuffled folds mix every month's calls. The latest calls show how it ranks calls made after the ones it learnt from, as it will be used.
Why standardize measurements before measuring distances between rows?
Unscaled, the measurement with the widest spread decides the distance. Among the beans, area in pixels outweighs every ratio, and the shape counts for almost nothing.
Which mean and standard deviation scale a new row?
The training rows', kept fixed, so a row gets the same scaled values whatever batch it comes in.
Why can NumPy and pandas give different standard deviations for the same column?
NumPy's std divides by the number of values and pandas' std by one less, unless told otherwise with ddof.
Why can nearest neighbours be found from squared distances?
Squaring keeps the order of distances, so the nearest rows are the same and no square root is needed.
Why sort distances with kind="stable" when finding nearest neighbours?
NumPy's default sort doesn't keep equal values in order, so rows at the same distance could come in any order. A stable sort keeps them in the training order.
How does scikit-learn's KNeighborsClassifier break a tied vote?
It takes the class that sorts first, whichever neighbour is nearer. Giving the tie to the nearest of the tied neighbours is a different rule, and gives different predictions.
Why can't the training rows choose k for nearest neighbours?
Each training row is its own nearest neighbour, so with k = 1 every one is named right, and fewer neighbours always look better.
What goes wrong with too few neighbours voting, and with too many?
Too few follow single rows, odd ones included. Too many bring in rows farther away, of other classes too.
After choosing k on a validation set, why measure accuracy on a test set?
The best of several tries on the validation set is optimistic, since the choice was made to suit it. Rows that took no part in the choice give a fair figure.
Why are new rows scaled by the training rows' statistics rather than their own batch's?
A batch's own statistics depend on what else it holds. A batch of one class is moved to the middle, and its rows land among the wrong neighbours.
How do you estimate a nearest-neighbour model's accuracy from its training rows?
Name each row by the others, leaving it out of its own neighbours. Counted among them, it votes for itself, and one neighbour scores every row right.
Where does NumPy's argsort put missing values?
After every number, in their order. A row whose distances are all missing gets the first rows as its nearest, unless it is caught first.
How can a nearest-neighbour model say an input is unlike anything it knows?
Compare the input's distance to its nearest training row with how far the training rows are from each other, and reject it when it is farther than any of them.
Why send split votes to a person?
The rows whose neighbours disagree are the ones the model gets wrong most often. Leaving them out raises the accuracy on the rest, at the cost of a person's time.
Can a distance threshold catch a new class that looks like a known one?
No: its rows are as near the known rows as those are to each other, so they are named as a known class. A new class needs training rows of its own.
What is a bootstrap sample of n rows?
n rows drawn at random from the n, with replacement: some rows come twice or more and others not at all.
What share of the distinct rows does a large bootstrap sample hold?
About , 63%, since each row is missed by all n draws with a chance of , near .
How does a bagged forest of trees score a row?
It averages the shares of yes its trees, each grown on its own bootstrap sample, give the row.
What does a random forest add to bagging?
Each split chooses among a few columns drawn at random, so the trees differ more from one another.
How many columns does scikit-learn give each split with max_features="sqrt"?
The square root of the number of columns, rounded down, drawn afresh for every split.
Why do bagged trees that choose among every column look alike?
The strongest column wins the first split in nearly every sample, and the splits below follow from it.
What are a tree's out-of-bag rows?
The training rows its bootstrap sample left out, about a third of them.
How can a forest estimate its AUC without a validation set?
Score each training row only by the trees whose samples left it out, and take the AUC of those shares.
Why can't a forest's AUC on its own training rows, scored by every tree, estimate its AUC on new rows?
Each tree scores rows it was grown on, which it remembers, so the AUC comes out far too high.
Why seed a forest's random generator once, rather than once for each tree?
Seeded once, each tree draws a sample of its own and the forest can be grown again exactly. Seeded for each tree, every tree draws the same sample.
On which rows should permutation importance be measured?
Rows the model wasn't trained on, such as validation rows. On its training rows, columns it only memorised look important.
Which trees may score a training row in a forest's out-of-bag estimate?
Only the trees whose bootstrap samples left that row out.
How does permutation importance measure a column?
By how far the model's score on rows it wasn't trained on falls when that column's values are shuffled among the rows.
Why can each of several correlated columns look unimportant?
Shuffled one at a time, each leaves the model the same information in the others. Shuffled together, they show what they carry.
What doesn't a column's permutation importance tell you?
Why the outcome happens: a column can matter to the model because it stands in for something else, such as when a call was made.
What does an autograd engine record as it computes?
For each value, the values it was made from and how to pass its gradient back to them, so that a result's backward pass can follow the records by the chain rule.
Why must an autograd engine add to gradients with +=?
A value used more than once gets a gradient from each use, and the chain rule adds them up.
Why does a backward pass go through the values in reverse topological order?
A value may pass its gradient on only after every value made from it has added its share, and the reverse order puts it after all of them.
How can an autograd engine get and without new derivatives?
By making them from operations it has, and , so the chain rule through those steps gives their gradients.
What is the derivative of , and why is it cheap in a backward pass?
: it comes from the output the forward pass already worked out.
Why does PyTorch's autograd record tensors rather than numbers?
A tensor operation stands for thousands of operations on numbers, and its forward and backward passes run in compiled code, so the engine's own work happens once a step.
What is the gradient of a tensor that broadcasting stretched?
The result's gradient summed back over the axes broadcasting added or stretched, since each of its values was used along them.
What are the gradients of a matrix product ?
for and for , where is the gradient of the product.
Why must a topological order skip a tensor it has already visited?
Without that, it lists a tensor once for each path to it, the backward pass runs its _backward for each entry, and the gradients come out wrong. Each residual block doubles the paths.
Why can't a test at 0 catch tanh's derivative written as ?
At 0, and are both 1. They part further from 0, and past 1 on either side is below 0, where tanh's slope never is.
What must unbroadcast sum besides the axes broadcasting added in front?
Each axis of length 1 in the tensor's shape that broadcasting stretched, keeping it with keepdims=True.
Why can't a recursive topological order handle a graph thousands of steps deep in Python?
Python stops a function that calls itself more than 1,000 deep with a RecursionError. A stack of the values still to visit does the same work with no call waiting on another.
How does a cross-entropy made of an engine's operations stay finite for large logits?
It takes each row's largest logit out first, as a constant, which changes neither the loss nor its gradient, so exp only sees numbers at 0 or below.
What loss should a classifier of classes start near, and why?
: before it learns, it gives each class about the same probability, , and the cross-entropy of that is .
Which bugs does learning one small batch by heart find, and which does it miss?
Broken gradients and steps, which keep the loss from falling towards 0. Not labels paired with the wrong pixels: a network learns wrong labels by heart too.
What does clip_grad_norm_ do to gradients whose total norm is above max_norm?
It scales all of them by the same factor, so the step keeps its direction and its length comes down to the learning rate times max_norm.
How can steps that are too large stop a ReLU network learning for good?
They can leave units below 0 for every input, and a ReLU below 0 passes no gradient back, so no later step moves the weights that feed them.
How does a learning-rate range test find the useful learning rates?
One short run, its learning rate raised by the same factor at every step, recording the smoothed loss: flat while the steps are too small, falling once they can learn, rising when they're too large.
Why train a few times below a range test's lowest point, not at it?
The lowest point is where the steps start to be too large: the office's network, trained at it, read fewer than 90% of the validation digits, and at half of it, 95.2%.
Labels paired with the wrong pixels: which check finds them, the batch learned by heart or the validation digits?
The validation digits. A network learns wrong pairs by heart too, and then reads only a guess's share of digits it hasn't seen.
Why must gradients added up over several batches go back to 0 at every step?
Otherwise each step carries every earlier step's gradients, and goes partly where old batches pointed instead of where its own do.
What should a training run's report hold, so that the run can be checked?
Its seed and settings, which train the same network again, and the measures it reports, which that network gives again when it's read.
Why count a run's batches through the function that gives it its gradients, not from its report?
A report says what its run claims. The function the run computes its gradients with sees every batch the run trained on.
Why is Kaiming's gain for a ReLU ?
A ReLU keeps half of a symmetric signal's mean square, and doubling the weights' mean square, a gain of , makes up for it, so on average each layer keeps the signal's size.
What does nn.Linear's own start keep of the signal through a ReLU?
A sixth of its mean square, about 0.41 of its size, layer after layer: deep networks started that way fade.
Why does a start a little too small or large matter more in a deeper network?
Each layer multiplies the signal's size by the same factor, so the error compounds: a start 10% too small leaves 0.04 of the size after 30 layers.
Which variance does batch normalization use in training, and which goes into the running estimate?
The batch's own, over , normalizes the batch. The unbiased one, over , goes into the running estimate.
What does batch normalization use in evaluation, and why?
The running estimates kept in training, so that a digit reads the same whatever else is in its batch, even alone.
What is BatchNorm1d's momentum of 0.1?
The weight each training batch's mean and variance get in the running estimates, which keep 0.9 of themselves at every batch: not an optimizer's momentum.
Along which axes do batch and layer normalization each normalize a batch?
Batch normalization each feature over the samples; layer normalization each sample over its features.
Why does layer normalization need no running estimates?
A sample's output depends on that sample alone, so it's computed the same way in training and in evaluation, for a batch of any size.
What does a batch normalization layer that uses the batch's statistics in evaluation give for a digit read alone?
Its bias, whatever the digit: a batch of one is its own mean, with a variance of 0. A network of such layers reads every digit alone as the same one.
Why does batch normalization's gradient with respect to a sample need more than the path through its own ?
Every sample moves the batch's mean and variance, which every uses, so its gradient has paths through both. With them, each feature's gradients sum to 0 over the batch.
Why does a linear layer before batch normalization need no bias?
Batch normalization subtracts each feature's mean over the batch, and the bias with it, so the bias's gradient is 0. shifts each feature instead.
With batch normalization after a layer, what does the scale of its weights still change?
The size of its steps. Weights times as large give the same outputs, aside, and get of the gradient, so for their size they move as far.
How does PyTorch's nn.Linear hold its weight?
With a row for each output and a column for each input, so its forward pass is .
What is a linear layer's gradient with respect to its weight, for a batch?
: the gradient at each output times the input it multiplied, added up over the samples. It has the weight's shape.
Why is a linear layer's bias gradient a sum over the batch?
The same bias is added to every sample's outputs, so each sample's gradient at the outputs adds to it.
What does a linear layer's backward pass return?
The gradient with respect to its inputs, , which the layer before it takes as the gradient at its outputs.
What is the gradient of a digit's cross-entropy with respect to its logits?
Its softmax probabilities less 1 at its label, . For the mean over a batch, each row is divided by the number of samples.
How do you keep the softmax's log-sum-exp from overflowing?
Take the row's largest logit out first: , where no term is above 1.
What loss does a network give when every logit is the same?
for classes, since each has probability . A first loss far above it is a sign the weights started too large.
What gradient does PyTorch's ReLU pass back where its input is exactly 0?
0, as for any input below 0.
Why does a network's backward pass run through its layers in reverse?
Each layer's backward pass needs the gradient at its outputs, which the layer after it works out.
How does PyTorch's nn.Linear start its weights and biases?
Drawn uniformly from to .
What happens to a ReLU unit whose input is 0 or below for every sample?
It passes nothing on and no gradient back, so it never learns again. Too high a learning rate can leave a whole layer like that.
Why is a test on a single sample a weak test of a loss's gradient?
With one sample a sum and a mean are the same, so a gradient that forgets to divide by the batch's size passes it.
Why does a PyTorch training loop call optimizer.zero_grad() before each backward pass?
PyTorch adds each backward pass's gradients to the ones already held, so without it every step would use the sum of every gradient so far.
Why can standardizing inputs give nan, and what's the fix?
A feature that never varies in the training data has a standard deviation of 0, and dividing by it gives nan. Divide such features by 1.
Why can a validation set overstate how well a model reads new people's handwriting?
When its digits come from the same people as the training digits, it measures how well the model reads those people. Only digits from other people measure new ones.
With momentum , how large does the velocity grow along a direction where the gradient stays ?
Toward : 10 times the gradient for = 0.9.
How does PyTorch's SGD start each momentum buffer?
At the first gradient, not at 0, so dampening starts at the second step.
Why does an optimizer change the parameters in place?
The network holds the same arrays, so it sees each step without anything being handed back.
Why does Adam divide its averages by ?
They start at 0, so after steps of a steady gradient the average is . The division gives back .
Without the bias correction, are Adam's first steps too small or too large with PyTorch's default betas?
Too large: starts further from its value than does, so a steady gradient's step is 3.16 learning rates at the first step and 6.6 at step 12.
How large is Adam's step for a steady gradient?
About the learning rate, whatever the gradient's size, since the step is a ratio of the gradient's average to the square root of its square's.
What goes wrong when Adam's weight decay is added to the gradient?
It's divided by with the rest of the step, so weights with large gradients are barely decayed and weights with small ones are pulled hard.
How does AdamW apply its weight decay?
It shrinks each weight directly before Adam's step, by the learning rate times the decay, and keeps the decay out of the averages.
Why doesn't decoupling the weight decay change plain SGD, without momentum?
Plain SGD neither averages nor divides its step, so the decay added to the gradient becomes the same shrink of .
Why does self.params[number] = self.params[number] - change stop a network learning?
It makes a new array for the optimizer and leaves the one the network holds as it was, so the network never sees a step.
When do with a step of , and with a step of , take different steps?
When the learning rate changes: the second keeps the steps taken at the old rate in its velocity for a while after a drop.
What must Adam's saved state hold for a run to go on as if it never stopped?
Its learning rate, its step count, and each parameter's averages of the gradient and of its square.
What does @dataclass write for a class?
__init__, __repr__ and __eq__, from the fields its body lists with their types.
What does frozen=True give a dataclass?
Instances that can't be changed, and a __hash__, so they can go in a set or be a dict's key.
What is @property for?
A method read as an attribute, as a JavaScript getter is, such as a value worked out from the fields.
What is self in a method?
The instance the method was called on, passed as its first argument, where JavaScript has this.
How does a subclass replace a method of its base class?
By defining a method of the same name, which Python finds first, on the instance's own class.
What does super().entries(value) call inside a method?
The entries of the class the current one inherits from, the one it replaced.
What's the difference between a class attribute and an instance attribute?
A class attribute, set in the class body, is shared by every instance. An instance attribute, set on self, belongs to one instance.
What does a | b call when a is an instance of a class?
type(a).__or__(a, b). If that returns NotImplemented, Python tries b's __ror__.
What should a special method such as __or__ return for an operand it can't handle?
NotImplemented, so Python can try the other operand's reflected method, and raise a TypeError when neither handles it.
What does x in y call when y's class defines __contains__?
y.__contains__(x). The answer is True when it returns a true value.
What should a class's __repr__ return, where it can?
A string that reads as the Python expression that would make an object with the same value.
In a Python class body, what does options = [] make?
One list, held by the class and shared by every instance that doesn't set its own. In a JavaScript class body, it's a field, made for each instance.
Does Python run the base class's __init__ when a subclass defines its own?
Only when the subclass's __init__ calls it, with super().__init__(...).
Why can't a dataclass that isn't frozen be counted with a Counter?
Comparing by its fields without being frozen, it gets no hash, and a Counter keeps what it counts as a dict's keys.
What does a class method get as its first argument?
The class it was called on, cls by convention, so cls(...) makes an instance of that class, a subclass included.
What does slots=True do for a dataclass?
It lists the fields in __slots__, so each instance keeps them in fixed places, without a __dict__, which takes less memory.
Which names in a dataclass's body become its fields?
Those with a type annotation. A name without one, such as struct = Object(...), stays a class attribute.
What is a closure?
A function that keeps the variables of the function it was made in, after that function has returned.
Does a closure keep a variable's value, or the variable?
The variable: if it names a list that's changed later, the closure sees the change.
What does f.__name__ give?
The name the function was defined with, such as 'number' for def number(value):.
What does x or y give in Python?
x if x is true, otherwise y, as JavaScript's || does.
What's the difference between re.search and re.match?
search finds a match anywhere in the string, match only at its start.
Why does a refinement test only values its check has passed?
So its test can count on the kind of value it gets, such as a number to compare with a threshold.
What does @decorator above def f do?
It replaces f with decorator(f), once, when the def runs.
What does functools.wraps(f) do for the function that wraps f?
It copies f's name and docstring onto it, and adds a __wrapped__ attribute that leads back to f.
How does a decorator take arguments, as in @define("code")?
define("code") runs first and returns the decorator, which is then applied to the function.
What does nonlocal count do in a nested function?
It lets the function assign to count in the function around it. Without it, assigning makes count a local variable of its own.
Why do the lambdas made in for low, high in rows: checks.append(lambda v: low <= v <= high) all use the last row?
Each lambda looks up low and high when it's called, and by then the loop has left them at the last row's values.
How do you make a lambda in a loop keep the loop's values as they are?
Bind them as defaults, lambda v, low=low, high=high: ..., or make each check with a factory function.
Why can't a cache key on a value alone when booleans and numbers must be told apart?
True == 1 == 1.0 and their hashes are equal, so a dict holds them under one key. Add the value's type to the key.
What does functools.lru_cache(maxsize=n, typed=True) do?
It keeps the results of a function's last n calls, with arguments of different types kept apart.
What kind of argument can't go through lru_cache?
An unhashable one, such as a list or a dict: its results are kept in a dict.
What runs when you call a coroutine function, as in check_code("S1", registry)?
Nothing in its body. The call makes a coroutine, which runs when it's awaited. A JavaScript async function starts running when it's called.
What does await do in a coroutine?
It waits for another coroutine's result, giving the event loop the chance to run other coroutines in the meantime.
Why does if registry.lookup(code): without await pass every code?
It tests the coroutine object, which is true, and never runs the lookup.
What does await asyncio.gather(*coroutines) give, and what happens when one raises?
Their results in their order. When one raises, gather raises its error at once and the others go on running, as with Promise.all.
What does a task group do when one of its tasks fails?
It cancels the tasks still waiting, waits for them, and raises the errors together in an ExceptionGroup.
What does return_exceptions=True change about gather?
Each error comes back among the results instead of being raised, as Promise.allSettled gives each rejection.
How do you hold coroutines to a limit of n at a time?
One asyncio.Semaphore(n) shared by them all, entered with async with around each one's work.
What does async with asyncio.timeout(seconds): do when the time runs out?
It cancels what the block is awaiting, and raises TimeoutError as the block ends.
Why async with gate: rather than acquire() and release()?
It gives the slot back however the block ends. A release() that an error skips leaves the slot taken, and the other tasks can wait for it forever.
Why does a task that raises after await gate.acquire() stall the others?
Unless a finally or async with gives the slot back, it stays taken, and the others wait for it forever.
How do you keep one code's error or timeout from failing a whole gather?
Catch it inside that code's coroutine, so it returns its own result and gather collects every other answer.
Why start a lookup's timeout inside async with gate: rather than around it?
Around it, the time a code waits for a slot counts against its lookup, and codes late in a long day time out before they're looked up.
Why can batches of limit lookups, gathered in turn, take longer than a semaphore of limit?
Each batch waits for its slowest lookup before the next starts. A semaphore gives a freed slot to the next code at once.
When a lookup raises an error the code doesn't handle, why use a task group rather than gather?
The group cancels the other lookups before it raises. gather raises the error and leaves them running.
Which errors does except StructError: handle?
Errors of that class and of every subclass of it, and no others.
When does a try statement's else clause run?
When the try clause raised nothing. The except clauses don't cover what runs in it.
What does super().__init__(message) do in an exception class's __init__?
It gives the error its message, which str(error) returns, and sets its args to (message,).
What does raise NewError(message) from error do?
It raises the new error with error as its cause, in __cause__, which a traceback shows before it.
What is error.add_note(note) for?
Adding a line to an error after it was raised, such as where it happened, without changing the error. A traceback shows its notes after its message.
What does except* StructError as group: do with an exception group?
It handles the group's StructErrors, as a group of their own, and the group's other errors go on to the caller.
What does with manager as value: call, and when?
manager.__enter__() before the block, giving value what it returns, and manager.__exit__(kind, error, traceback) however the block ends.
What happens when a context manager's __exit__ returns True?
The error the block raised is suppressed, and the code after the with statement runs as if nothing had been raised.
In a @contextmanager generator, where does an error raised in the block appear?
At the yield, so a try around the yield can handle it, or a finally can clean up after it.
Why must except StructError come before except TypeError?
A StructError is a TypeError, and the first except clause that matches takes the error.
Does except StructError: handle an ExceptionGroup of StructErrors?
No. It matches the group's own class, and only except* StructError reaches the StructErrors inside it.
What does raise NewError(message) from None do?
It raises the new error without a cause, and hides the error being handled from the traceback, though __context__ still holds it.
In what order does with a, b, c: leave its context managers?
c, then b, then a: the statement works as if each were inside the one before it.
Why open a file in a with statement rather than closing it after the loop?
with closes it however the block ends. A close() after the loop doesn't run when the loop raises an error.
Why pass encoding="utf-8" to open?
Without it, a file is read in the machine's default encoding, which can differ from one machine to the next.
How do you write JavaScript's xs.filter(g).map(f) in Python?
As a list comprehension: [f(x) for x in xs if g(x)].
What does def f(*args) do with the arguments it's called with?
It gathers them into a tuple, as ...args gathers them into an array in JavaScript.
In what order do two for clauses in one comprehension run?
As nested loops do, the first one outermost.
Where does a key go when a dict is given it a second time?
It stays where it first went in, with the later value.
What does calling a function that contains yield return?
A generator. The function's body runs only as far as the next yield each time a value is asked for.
What does yield from other do?
It yields each value other yields, in turn, as a loop over other with a yield in it would.
What does return do inside a generator?
It ends the generator: there are no more values to ask it for.
How do you make a tuple with one more item than path?
With (*path, key), a new tuple, which leaves path as it was.
What does next(it, None) give when it has no values left?
None, where next(it) would raise StopIteration.
How do you take only the first n values of an iterator?
With itertools.islice(it, n), which asks for no values after the first n.
What happens when you loop over the same generator twice?
The second loop finds nothing: a generator gives each value once.
What does enumerate(xs) give?
Each item of xs with its index, counted from 0, as (index, item) pairs.
What runs when you call a generator function and don't read from what it returns?
Nothing: the call only makes a generator, and its body runs as values are read.
What does any(generator) leave in the generator?
Whatever comes after the first true value, which any has read and kept no copy of.
How does a list comprehension differ from a generator expression in what it reads?
The comprehension reads everything at once to build its list. The generator expression reads one value each time it's asked for the next.
How do you go through a file's lines without reading the whole file?
Loop over the open file: it gives one line at a time, read as it's asked for.
Why turn lines into records with a generator expression rather than a list comprehension?
The generator makes each record when it's asked for, so only one is held at a time. The list makes them all first.
What's the difference between a parameter typed object and one typed Any?
mypy allows only what works on every value with object, until code checks what it is. Any switches checking off, as TypeScript's any does.
How do you write a tuple type of any length whose items are strings or ints?
tuple[str | int, ...]
How do you mark a TypedDict key that a record may leave out?
NotRequired[str], as TypeScript's note?: string does. d.get("note") gives its value or None.
Does Python check a function's annotations when it calls it?
No. Python never reads them as it runs; a type checker such as mypy reads them beforehand.
How do you write a generic class in Python 3.12 and later?
class Struct[T]:, with T in its annotations. mypy works out T from how each one is made, as TypeScript does for class Struct<T>.
What does a function that returns TypeGuard[T] tell mypy?
That where it returns True, its argument is a T. Where it returns False, mypy learns nothing.
When is it safe to type a predicate with TypeIs rather than TypeGuard?
When it's True for every value of its type: TypeIs also narrows where it returns False, to rule that type out.
How do you run one pytest test for several inputs?
@pytest.mark.parametrize("value", [...]) above it, with value as its argument: it runs once for each.
What does with pytest.raises(StructError) as raised: check?
That the block raises a StructError or an error of a subclass. It fails when the block raises nothing, and raised.value is the error.
How can you tell whether your tests would catch a bug?
Put the bug in on purpose and run them: a test that still passes doesn't test that behaviour.
What does cast(Sample, record) do as the code runs?
Nothing: it returns record as it is. It only tells the type checker to treat it as a Sample, so the code must have checked it first.
Does isinstance(18, float) hold, and does an annotation of float take 18?
No, and yes. The type system takes an int where float is annotated, but an int isn't a float.
What does uv.lock hold that pyproject.toml doesn't?
The exact version of every package the project installs, the packages its dependencies need included, for every platform and Python version it supports.
Why compare package names with canonicalize_name?
The standards count names that differ only in case, or in runs of -, _ and ., as one name: PyYAML is pyyaml, and python_dateutil is python-dateutil.
Which versions does ~=2.4.0 allow?
2.4.0 and every later 2.4 release, as >=2.4.0 with ==2.4.* does.
Is True an int in Python?
Yes: bool is a subclass of int, so isinstance(True, int) is True. A check for numbers rules booleans out first.
What does json.loads make of the JSON numbers 3 and 3.0?
The int 3 and the float 3.0. JavaScript reads both as the same number.
How do you test whether a float is NaN?
With math.isnan(x). NaN equals nothing, not even itself, so x == float("nan") is always false.
What does !r do in an f-string?
It writes the value as repr does, so a string keeps its quotes: '42' and 42 look different.
What does a is b test, and when should you use it?
Whether a and b are the same object. Use it for None, True and False, and where identity is the point, never to compare numbers or strings.
Why is True in [0, 1] true in Python?
in compares each item with == as well as is, and True == 1, since bool is a subclass of int.
What does JavaScript's === mean for two objects?
That they are the same object, as Python's is does. Two arrays with the same items aren't ===.
What kind of copy does dict(record) make?
A shallow copy: a new dict whose values are the same objects, so a list in it is shared with the original.
Why is def f(tags=[]) a trap?
The default is made once, when def runs, so every call that leaves tags out shares one list. JavaScript makes a default afresh on each call.
How do you give each record its own empty list by default?
With a factory, a function that makes the value, such as list, called once for each record.
Which values are falsy in Python but true in JavaScript?
Empty lists, dicts and other empty collections, such as [] and {}.
In what order does a dict give its keys?
In the order they were added. A set's order can change from one run to the next.
Why can value is 1000 be false for a value equal to 1000?
is asks whether they are the same object, and 1000 read from JSON is a new object. Compare values with ==.
What does result = record copy?
Nothing: it gives the same dict a second name, so changing result changes record.
Why is if not record.get(key) no test of whether a field is missing?
It's true for a field set to 0, "", False or an empty list too. key not in record is true only for a missing field.
What does copy.deepcopy copy that dict(record) doesn't?
The lists and dicts inside the record, at every level. dict(record) copies only the first.
When is it harmless for two records to share a value?
When the value can't be changed in place, as a number, a string, True or None can't. A shared list or dict changes for both.
What is a tuple for?
A fixed group of values whose parts belong together, such as a field and its message. It can't be changed once made.
How many positions across does nn.Conv2d(1, 16, 3, stride=2, padding=1) take on a photo 28 pixels wide?
14: .
Why does a convolution hold so few parameters next to a dense layer?
Each filter takes the same weights to every position of the photo, so its parameters don't grow with the photo's size.
What does groups=C give a convolution over channels?
A depthwise convolution: each channel gets filters of its own, and the layer holds of the weights.
Where does max pooling send its gradient?
To the pixel each window kept, the first largest when several tie. The window's other pixels get none.
What does the magic number at the start of an idx file give?
Its third byte gives the type of the data, 8 for unsigned bytes, and its fourth the number of dimensions, whose sizes follow it.
In what shape does a convolution take its input, and how does a batch of grey photos get it?
(N, C, H, W). Grey photos have one channel, which unsqueeze(1) adds to a batch of shape (N, H, W).
Why check an idx file's data against its header?
A file cut short, or the wrong file, would otherwise give tensors of the wrong shape or kinds out of step with their photos, and training raises no error on either.
What does X.to(device) do that net.to(device) doesn't?
It gives X on the device, a copy unless it's there already, and leaves X where it was. net.to(device) moves the network's own parameters and buffers in place.
Under torch.autocast with bfloat16 on the CPU, what runs in bfloat16, and what stays float32?
Convolutions, linear layers and matrix multiplies run in bfloat16. Cross-entropy runs in float32, and the weights and their gradients stay float32.
Why does bfloat16 training need no loss scaling, where float16 training does?
bfloat16 has float32's 8 bits of exponent, so small gradients keep their range. In float16 they lose precision below 0.000061, and far smaller ones round to 0.
What is a tensor on the meta device?
A shape and a dtype with no data: a forward pass there works out every shape and computes nothing.
What share of the shop's network's parameters sit in its convolutions, and what share of its multiply-adds do they do?
23% of the parameters, and 98% of the multiply-adds: their weights work at every position.
What does nn.Sequential(*[Block(16, 16)] * 2) hold?
One block, twice: the list repeats the same module, so both places run it with the same weights.
What does F.cross_entropy take, and what goes wrong if a network ends in a softmax?
Logits: it applies its own log-softmax. Given probabilities, it takes a softmax of them, and the loss can't fall below 1.46.
Why does mixed precision keep the weights in float32?
In bfloat16, a weight moves in steps of 0.39% to 0.78% of its size, so smaller updates round away. Autocast runs the convolutions and linear layers in bfloat16 and leaves the weights in float32.
What does OneCycleLR do to the learning rate?
It raises it along a cosine from a 25th of its peak to the peak over the first 30% of the steps, then lowers it along a cosine to far below where it started, and moves the momentum the other way.
Why does batch norm let a network train at a higher learning rate?
It makes each layer's gradients depend less on the scale of the weights before it, so larger steps don't make training diverge.
Why read the test photos only once, at the end?
Choices made by looking at them fit them, and they'd no longer measure how the classifier does on photos it hasn't seen. Choices are tried on photos held out of the training set.
What does register_buffer give a tensor that a plain attribute doesn't?
A place in the module's state: state_dict() holds it and .to() converts it. Unlike a parameter, it isn't in parameters(), so no optimizer trains it.
Why keep a network's layers in nn.Sequential or nn.ModuleList, not a Python list?
Modules in a Python list aren't registered: their parameters aren't in parameters() or state_dict(), and train() and eval() don't reach them.
What does nn.BatchNorm1d normalize by in training, and in evaluation?
In training, the batch's own mean and variance, which it also mixes into its running estimates. In evaluation, the running estimates, which it leaves alone.
Why give a DataLoader a torch.Generator of its own?
Its orders then depend on the seed and the epochs dealt so far, and nothing else. Without one, it draws from the global generator, which every other random draw moves.
What do model.eval() and torch.no_grad() each switch off?
eval() puts layers such as batch normalization and dropout in evaluation mode. no_grad() stops autograd recording a graph. Reading for validation needs both.
What must a checkpoint hold for a run to go on exactly as it would have?
Everything its next step depends on: the model's state dict, the optimizer's, the state of every generator the run draws from, and how many epochs it had done.
What does SGD with momentum keep in its state?
A momentum buffer for each parameter, the running sum of gradients its steps follow. An optimizer started again without it steps differently.
Why load checkpoints with torch.load's default, weights_only=True?
It builds only tensors and plain types and imports nothing, where a full unpickle can be made to run any code. Even so, load only files you trust.
What goes wrong when a network with batch normalization validates in training mode?
Each norm normalizes the validation digits by their own statistics and mixes them into its running estimates, so the digits kept back to judge the network change it.
How can you tell a checkpoint holds everything a run needs?
Stop a run, pick it up from the checkpoint, and compare it with a run that never stopped. On the same machine and PyTorch, with deterministic operations, the weights should match to the last bit.
Why doesn't best = model.state_dict() keep the best epoch's weights?
state_dict() holds the model's own tensors, so every later step changes what it holds. copy.deepcopy(model.state_dict()) keeps a copy.
How do you save a checkpoint so that a stop while saving leaves the last one whole?
Write it to a new file beside the old one, then move it over the old one with os.replace. A stop before the move leaves the old one as it was.
When can view give a tensor a new shape?
When its strides fit the new shape. A transposed tensor isn't contiguous, so view can't flatten it, and reshape copies it instead.
How does PyTorch's std differ from NumPy's by default?
PyTorch divides by and NumPy by . correction=0 gives NumPy's.
How does torch.from_numpy differ from torch.tensor?
torch.from_numpy shares the array's memory, so a change to one changes the other. torch.tensor copies it.
What does loss.backward() do with a gradient a parameter already holds?
Adds the new gradient to it, in .grad. A training loop clears .grad each step, or the steps' gradients pile up.
Why does a training step change the parameters inside torch.no_grad()?
So autograd doesn't record the step in the graph. Made in place, the step keeps each parameter the same leaf tensor.
What does torch.autograd.grad give that backward() doesn't?
The gradients themselves, returned, without adding them to any .grad.
What does a custom autograd function's backward receive, and what does it do with it?
grad, the loss's gradient with respect to the function's output, which it multiplies by the function's own gradient, by the chain rule.
What does a custom autograd function's backward return for an input with no gradient, such as integer labels?
None, in that input's place.
Why does gradcheck work in float64?
It compares the backward pass with finite differences, and in float32 those lose too many digits to tell a wrong gradient from rounding.
What does .detach() on a layer's outputs do to the layers before it?
It cuts them out of the graph, so the loss has no gradient for their parameters and they never train.
How can training a tensor made with torch.from_numpy change the array it came from?
The tensor shares the array's memory, so a step made in place on the tensor writes to the array.
Why does the port draw its start from the NumPy reader's rng, not from torch.manual_seed and nn.Linear?
To start from the same weights. A reader started any other way trains to a different network, which reads some digits differently.
What does reading under torch.no_grad() save?
Autograd records nothing, so no tensors are kept for a backward pass that never comes.
Why number a dataset's kinds by their sorted names, rather than in the order its files list them?
Files can be listed in any order, and another copy can list them in another. Sorted names give each kind the same number from any copy, so a model's outputs are read as the right kinds.
Why sort the names before picking tiles from them at random?
The tiles picked then depend only on the names and the seed, so the same seed picks the same tiles from a copy that lists them in another order.
Why pick as many tiles of each kind for a test set?
Accuracy then weighs every kind the same, and a classifier that always gives one kind is right on 10% of them.
In what order of dimensions do Pillow's pixels come, and in what order does PyTorch take a batch?
Pillow, through NumPy, gives rows, columns, channels. PyTorch takes (N, C, H, W), which permute(0, 3, 1, 2) gives from a stack of (N, H, W, C).
What does weights_only=True guard against when loading a checkpoint?
A checkpoint is a pickle, which can run code as it loads. With it, torch.load rebuilds only tensors and plain containers, and refuses the rest.
What does strict=True catch when weights are loaded into a model?
Names that don't match: a parameter the checkpoint lacks, which would keep its random starting values, or a weight with no parameter to go to.
Why must an image's sides be multiples of 14 for DINOv2's ViT-S/14?
It cuts images into patches of 14 by 14 pixels, which must cover the image.
Why are tiles normalized with ImageNet's mean and standard deviation before DINOv2?
The backbone learned from images normalized that way, so its layers expect inputs on that scale.
Why can't features computed under torch.inference_mode() train a layer?
Its tensors can't be saved for a backward pass, which the layer's gradient needs. torch.no_grad() gives ordinary tensors.
What does a linear probe learn, and from what?
A linear layer that gives each kind a score, learned from a frozen backbone's features of a few labelled examples.
Why does a probe keep the mean and deviation it standardized the labelled tiles with?
A new tile is standardized as they were, so its scores don't depend on the tiles scored with it.
Why does a probe on 200 tiles of 384 features need weight decay?
Planes can always split so few points in so many dimensions, so without decay the weights grow without end as the loss falls toward 0. With it, the loss has one lowest point.
What is the standard error of a share right of test tiles?
: on another tiles like them, the share usually comes within about twice that of .
Why does fine-tuning give a pretrained backbone a smaller rate than its new head?
AdamW moves each weight by about its rate at every step, so at the head's rate the backbone soon leaves the weights that made its features good.
A layer's parameters aren't in the optimizer, but requires_grad is on. What does a training step cost for them?
Autograd still works out and keeps their gradients, at about twice the work of their forward pass, and nothing uses them.
What goes wrong when a model is trained on inputs prepared one way and used on inputs prepared another?
It learned from features unlike those it's given in use, so its scores no longer mean what they meant in training.
What do DINOv2's own linear classifiers take from the backbone?
The class token, and beside it the mean of the patches' tokens.
Why do larger tiles give a vision transformer better features, and what do they cost?
More patches see each tile in finer pieces. The work grows with the tokens, and attention's with their square.
Why do a satellite tile's turns and mirror images show the same thing to a classifier of land use?
Seen from above, the ground has no up and no left, so each of the square's 8 symmetries shows the same land.
Why keep each chunk as its place in its page, not just its text?
Its place shows where it sits in the page and which characters of an answer it holds; two chunks with the same text can come from different places.
What do windows that share words buy, and what do they cost?
A passage cut by one window's edge can be whole in the next; every shared word is embedded and stored twice.
How do blocks joined to a size differ from windows of that size?
Joined blocks cut only between headings, paragraphs and list items, so their sizes vary; windows cut wherever the count of words falls.
Why score a search by the characters of the answer it finds, not by passages marked relevant?
Characters score any way of cutting the pages on the same questions; passages marked relevant score only searches through those passages.
Scored by an answer's characters, what do a search's recall and precision measure?
Recall: the share of the answer's characters its chunks hold. Precision: the share of its chunks' characters that are the answer's.
Why is precision low even for a search that finds exactly the chunks holding the answer?
An answer is a sentence or two, and each chunk brings the rest of its text with it: the larger the chunks, the lower the precision.
Why compare chunkings at one budget of words, rather than at one number of chunks?
The model reads every word a search hands it. At one number of chunks, larger chunks hand it more text, and find more for that alone.
At 400 words, which chunkings of the help desk's pages found the most of each answer?
Those of 100 words: windows of 100 words found 0.598 of an answer on average, and the passages of 200 words 0.550.
At 400 words, did windows that share words find more than windows that don't?
Hardly: windows of 200 words sharing 50 found 0.561 of an answer on average, and those sharing none 0.558.
Why search for each chunk's place from where the chunk before it ended?
A page can hold the same text twice; searching from its start finds the first copy, so a later chunk takes an earlier one's place.
Which chunk does a chunker that joins blocks most easily lose?
The last one: the chunk still being joined when the page runs out, unless the code adds it after the loop.
What can be checked of a chunker on every page, whatever the page holds?
That it keeps every word, in order; that no chunk passes its size; that every chunk is the page's own text; and that each is at its own place.
Why does a sentence added early in a page change every window of words after it?
Each window starts a set number of words after the one before, so every boundary after the edit moves, and every window there holds new text.
How does starting a new chunk at each heading keep an edit's cost down?
No chunk holds text from two sections, so an edit changes only the chunks of its own section, and the rest keep their vectors.
What does a cache of vectors keyed by each chunk's text save when a page changes?
Every chunk that comes back with the same text keeps its vector; only chunks with new text are embedded again.
What does nomic-embed-text-v1.5 need at the start of each text it embeds for a search?
What the text is for: search_document: before a passage to search, and search_query: before a question.
Why does an embedding client send its texts in batches, not all in one request?
A text the server can't embed fails its whole request, so batches keep a failure to a few texts, and each request and reply small.
How does an embeddings reply say which vector belongs to which text?
Each item in data has an index: the place of its text in the request's input.
What must an embedding cache's key hold?
Everything the vector comes from: the model's name, the prefix and the text.
Why does the cache commit each batch as soon as it's embedded?
So that a failed request or a Ctrl+C keeps every batch before it, and the next run embeds only the rest.
How many times does the cache embed a text that comes twice in one call?
Once: both places get the vector of its one key.
How does nomic-embed-text-v1.5's card cut a vector to fewer numbers?
Layer normalization across all its numbers, then the first numbers kept, then scaled to a length of 1.
What does quantizing each number to a byte need from the passages?
Each number's low and high across them: the byte is one of 256 steps between the two.
Why clip before astype(np.uint8) when quantizing?
Cast to uint8, a value past 255 has no defined result, and machines differ in what they give.
What happens when an embedding search sends its questions with the documents' prefix?
Nothing fails: the search still finds passages, just fewer that answer the questions.
Why scale an embedding cut to its first numbers back to a length of 1 before scoring?
The first numbers' length differs from one vector to the next, and a dot product grows with it, so of two vectors at the same angle to a question, the longer would score higher.
What does np.argsort(scores)[-k:] give?
The k highest scores' places, with the highest last: np.argsort sorts from the lowest.
Why can an index keep 256 numbers of a byte each and still find nearly every answer the full vectors find?
The model was trained so that its vectors' first numbers work as a vector of their own, and 256 steps for each number lose little of what a float32 keeps.
What does an index that quantizes its vectors need to keep beside the codes?
Each number's low and high, without which the codes can't be turned back into the numbers they stand for.
What does the hit rate at k ask that recall at k doesn't?
Whether any relevant passage is among the first k. Recall asks what share of the relevant passages are; the two are the same for a question one passage answers.
Why must a run's mean count a question the search showed nothing for?
Left out, it lifts the mean: the confident search's hit rate at 5 would rise from 0.746 to 0.761, above the search it keeps a part of.
In what order does trec_eval read the passages of a run?
In the order of their scores, highest first, a tie broken by the passages' ids. It ignores the ranks the run gives them.
Why judge a search's passages by their texts when some passages are copies?
A second copy of an answer adds nothing for the reader, yet judged by passage it adds to recall and nDCG, and a search that leaves the copies out loses.
Which measures are the same judged by passage and by text?
The hit rate and the reciprocal rank: each counts only the first relevant passage, and the first copy of a text is never a repeat.
Judged by passage and then by text, how did fused and fused-once compare by nDCG at 10 on the validation questions?
By passage, fused came first, 0.602 against 0.591; by text, fused-once, 0.610 against 0.600.
Why compare two searches question by question?
They answer the same questions, and a question both answer or both miss says nothing about which is better. A paired test keeps each question's two results together.
What does a randomization test swap, and what is its p-value?
Each question's two values, at random, since the same two searches could as well have given them the other way round. The p-value is the share of the resamples whose difference is as far from 0 as the observed one, or further.
On the help desk's test questions, which services' intervals show the reranker's gain in hit rate?
Only dmv's. With 66 to 118 questions a service, the other three take in 0, and of the 20 questions the reranker gains on balance, 13 are in dmv.
Tested as if each search had questions of its own, what p-value did the gain of reranked over fused-once get?
0.0778, above 0.05, where swapping each question's own two values gives 0.0062: the questions both answer or both miss widen what chance could give.
Why does dropping a repeated text from the passages shown lift a search's hit rate?
The passages after it move up, so one the reader would find below the first 5 counts among them: the hit rate of fused came out at 0.762 where it is 0.754.
What does judging nDCG by every copy of an answer do to a search that shows the answer first?
Holds it below 1, since the best ranking would show every copy: the nDCG at 10 of reranked came out at 0.643 where it is 0.668.
What must a new search show before the help desk's release check ships it?
A higher hit rate at 5 over every test question, a randomization test's p-value below 0.05, and no service whose hit rate falls by more than 0.03.
Run a step at a time on the help desk's test questions, which of its changes did the release check ship?
The vectors over BM25, and the reranked search over the fused ranking. It held fusion over the vectors, and each text once over the fused ranking: chance could give their gains at 5.
Why weigh a search against the one it replaces rather than a step at a time?
A change whose gain comes with a later step is held a step at a time: the reranked search ships over the vectors alone, where fusion on its own doesn't.
Why does a generated tsvector column need STORED before Postgres can index it?
Postgres makes a generated column virtual unless it says otherwise, worked out each time it's read, and a virtual column can't be indexed.
How does websearch_to_tsquery join a question's words?
With AND, unless or stands between two of them: a passage must hold every word. Of the help desk's 500 validation questions, 370 found no passage that way.
What does ts_rank_cd rank a passage by, for a question's words joined by OR?
How many times the words appear in it, 0.1 for each. A common word counts as much as a rare one, since Postgres's ranking uses nothing about the other passages.
In BM25, why does a word that few passages hold count for more than a common one?
Its idf weighs its count by how few passages hold it. In the help desk, the commonest word, “the”, in 2,310 of the 2,419 passages, weighs 0.046, and a word one passage holds 7.39.
Why does Lucene add 1 inside BM25's logarithm?
Without it, a word that more than half the passages hold weighs less than zero, and lowers the score of every passage that holds it.
What do BM25's and set?
, how fast a word's count levels off; , how much a passage's length takes away, none at 0 and in full at 1.
Why fuse BM25's ranking and the vectors' by rank rather than by score?
Their scores are on scales of their own, so a sum would be decided by the larger. Ranks are on one scale.
What does reciprocal rank fusion add up for a passage?
for its rank in each ranking, the first rank being 1. Cormack, Clarke and Büttcher set to 60.
On the help desk's validation questions, how did the fused ranking compare with the vectors'?
It found more among its first 20, 0.856 against 0.844, and less among its first 5, 0.684 against 0.712.
Why must a search read a question into words exactly as it read its passages?
A word matches only the same word: a question's capitalised words find nothing among passages whose words were lowercased. The assistant's search lost 1,169 of the 6,657 words of the validation questions that way.
Why can't a hybrid search add a passage's BM25 score to its cosine?
The scores are on scales of their own: a question's best passage scores 8.4 by BM25 at the median, and a cosine is at most 1, so BM25 decides the sum. Fuse their rankings by rank instead.
Why multiply vectors stored as 16-bit whole numbers in 64 bits?
Their products overflow 16 bits, and NumPy wraps a 16-bit result around without a word: a dot product of 897,101,185 came out at -21,119.
Why does a search rerank only a fused ranking's first passages, rather than every passage?
A reranker reads the question and a passage together, so it takes a run of the model for each passage it scores. Vectors, made before the question, search every passage at once.
What does Qwen3-Reranker give as a passage's score?
The probability it gives the answer yes, asked whether the passage meets the requirements the question sets. It orders one question's passages; nothing says it compares across questions.
In what order does llama.cpp's server give a rerank request's results?
By score, highest first, not in the order the documents were sent: each result's index names its document.
On the help desk's validation questions, what did reranking the fused ranking's first 20 texts do to the hit rate at 5?
It rose from 0.708, the fused ranking's with each text once, to 0.756.
What does pgvector's <=> operator give?
The cosine distance between two vectors: 1 less the cosine of the angle between them, so the nearest has the smallest.
Why keep a table's vectors in their rows, with STORAGE PLAIN?
Postgres otherwise moves them out of rows over 2 kB, into the table's TOAST table, and its planner leaves that out of what reading every row costs, so a scan can look cheaper than a faster index.
Why load many rows of vectors with COPY in its binary format?
One statement streams every row, and each vector goes in its float32 bytes, with no decimal text to write and read back.
Why run ANALYZE on a table after loading many rows into it?
The planner chooses each query's plan from the statistics ANALYZE gathers, such as how many rows the table has; with none, it can choose badly.
What does pgvector's hnsw.ef_search set?
The size of the candidate list an HNSW search keeps in the graph's lowest layer: a larger one finds more of the exact search's nearest, more slowly.
Why set hnsw.ef_search for one search with set_config(..., true) in a transaction?
It lasts until the transaction ends, as SET LOCAL does, so it changes that search alone, and unlike SET it takes the value as a parameter.
Which queries can an HNSW index built with vector_cosine_ops serve?
Those ordered by embedding <=> query, nearest first, with a LIMIT: by its own operator, not an expression of it.
Why can a search through an HNSW index with a WHERE return fewer rows than its LIMIT?
Postgres applies the WHERE after the scan, to the candidates the index finds, hnsw.ef_search of them, and keeps only those that match.
How do pgvector's iterative scans in strict_order and in relaxed_order differ?
Both go on scanning until the query has its rows. Strict returns them in order of distance, leaving out any found nearer than one already returned; relaxed keeps those, slightly out of order, and finds more of the nearest.
Why sort a relaxed scan's rows by distance + 0, outside a materialized CTE?
Its rows can come slightly out of order, and Postgres 17 and later take a plain ORDER BY distance over the CTE as already done by it; + 0 makes Postgres sort.
Why can't an index serve ORDER BY 1 - (embedding <=> %s) DESC?
An index serves an ORDER BY of its own operator on the column, nearest first; this sorts by an expression of the distance, so Postgres reads every row and sorts them.
What does pgvector's <#> give, and which order puts the most alike first?
The inner product times -1, so ascending order, as with every pgvector distance, puts the largest inner product first.
Why send a filter's value to Postgres as a parameter, not in the query's text?
Text in the query runs as SQL: a value with a quote in it can break the query or change what it does.
How can a vector index take less memory and still find what the full vectors find?
Index a smaller expression of each vector, such as its first numbers at half precision, take more candidates from it than you need, and sort them by the full vectors, kept in the table.
When does Postgres go through an index on subvector(embedding, 1, 256)::halfvec(256)?
When the query orders by that same expression with the operator the index's class serves, nearest first, with a LIMIT.
How do you add a row, or change the one with its key, in one statement?
INSERT ... ON CONFLICT (id) DO UPDATE SET column = EXCLUDED.column, for every column that can change.
Why is a change to an applied migration refused, rather than applied again?
The databases that ran the old SQL would keep a different schema from the ones that run the new SQL, so a change is always a new migration.
What keeps two copies of the app that start at once from applying a migration twice?
An advisory lock on a key every copy shares: the second waits until the first has finished, then finds every migration already applied.
Why does keep lock the conversation's row before it counts the messages?
Two turns kept at once would otherwise count the same messages and write at the same position; with the lock, the second counts once the first has committed.
Why are the pool's connections in autocommit mode?
Otherwise the first statement opens a transaction that stays open, holding locks, until a commit; in autocommit, a transaction is a block that says where it starts and ends.
Why does the async replies route call the store through run_in_threadpool?
psycopg's calls block while they wait for Postgres; on the event loop, each would hold up every other caller's stream.
Why is the store's work in finally inside anyio.CancelScope(shield=True)?
A caller who leaves cancels the route, and anyio cancels again at every await, so without the shield the turn would never be kept.
What can an id do when it's written into a query's SQL with an f-string?
Change the query: an id written as SQL, such as x' OR 'x'='x, can read another caller's conversation.
Why take a pooled connection with with pool.connection() rather than getconn and putconn?
The block gives it back however it ends, an early return or an error included; putconn gives it back only on the paths that call it.
Why does the usage query group by (started_at AT TIME ZONE 'UTC')::date, not started_at::date?
started_at::date takes the day in the session's time zone, which a database, role or connection can each set; AT TIME ZONE 'UTC' gives the same day whoever asks.
Why does the usage query compare started_at itself with the start of the first day?
The index on started_at can find a range of started_at, but not of a value worked out from it, which would mean reading every turn.
Why does each of the desk's log lines hold one JSON object?
A host can filter JSON on any field, such as every line of one request or every error, and a traceback inside the object keeps the record on one line.
Why does the desk keep the request's id in a context variable, not a global?
Each request runs in its own asyncio task, which copies the context, so requests answered at once each see their own id.
What does the desk's request line leave out of the path, and why?
The query, which can hold a sign-in code, a token or a person's details, none of which belong in a log.
What's the difference between the desk's /health and /ready?
/health says the process is up and asks nothing else, so a host restarts the desk only when the desk itself is stuck. /ready also checks the database and the model, so a host sends callers only while they answer.
Why does the readiness check ask the model's server for /health, not for a reply?
The server says whether the model has loaded without generating anything, so a check every few seconds takes no slot from callers.
Why does each readiness check get two seconds, all at once?
A host waits only a few seconds for an answer, so /ready has to answer in time to say which check is stuck, whatever hangs.
Why does the desk listen on 0.0.0.0 in a container, not uvicorn's default of 127.0.0.1?
Requests reach the container over its network, and only the container itself reaches 127.0.0.1.
Why does the desk believe X-Forwarded-For only from the proxies it's told to trust?
Any caller can send the header, so trusting it from everyone lets a caller choose the address the desk sees.
Why does the desk give open requests 25 seconds once it's told to stop?
Hosts such as Render kill the process 30 seconds after SIGTERM, and the limit leaves time for the desk's shutdown to close its pool and clients first.
A sign-in callback's address is /auth/callback?code=...&state=.... What does that mean for a request log?
Logging the whole address writes each sign-in's code into the logs, so the request line logs the path without its query.
Why doesn't a pool's connection timeout keep a readiness check from hanging?
It limits only the wait for a connection. A query to a database that has stopped answering can wait on, so each check needs a limit of its own.
Why does a desk that listens on 127.0.0.1 get no requests in a container?
Inside a container, 127.0.0.1 is the container itself, and the host's proxy reaches it over the container's network, so the desk has to listen on 0.0.0.0.
What does a multi-stage Dockerfile leave out of the desk's image?
Whatever only the build needed, such as Node, npm, uv and the test packages: the last stage gets only what's copied into it.
Why is the desk's CMD in exec form, ["python", "main.py"]?
The shell form runs the program under /bin/sh -c, which doesn't pass signals on, so the desk would never get the host's SIGTERM and could never shut down cleanly.
Why does the image install packages with uv sync --locked?
The build fails if the lock is out of date, so the image has the versions the tests ran or doesn't build at all.
Why does the desk send the S256 challenge of its PKCE verifier, and not the verifier?
The authorization request passes through the browser, where others can read it. Only the desk holds the verifier, which it sends to the token endpoint with the code, so a code someone else takes is no use to them.
Why is a discovery document that names another issuer refused?
Its addresses aren't the provider the desk was set up for, and its ID tokens would name that other issuer, so nothing it gives can be trusted.
Why does each sign-in get a new state, nonce and verifier?
They tie the provider's answer to one request from one browser. Values used twice, or shared, would let an answer to another request pass as this one's.
Which of an ID token's claims does the desk check, and against what?
iss against the provider's issuer, aud against the desk's client id alone, exp against the time now, and nonce against the one its sign-in sent.
Why is algorithms fixed at RS256, rather than read from the token's header?
The header is written by whoever made the token. Read from it, the algorithm could be none, or one that reads the key another way, as PyJWT's documentation warns of mixing HS256 and RS256.
When does the desk fetch the provider's keys again?
When a token names a key id it doesn't hold, as after the provider rotates its keys. Otherwise it keeps the keys it has.
Why is a person kept by their issuer and subject, and not their email?
The issuer and subject together are the only claims OpenID Connect promises are stable and unique. A provider may give an email to someone else later, and a person's email can change.
Why does the database keep each session token's SHA-256, rather than the token?
Someone who reads the database, or a backup of it, gets only hashes, which sign no one in. A fast hash is enough, since the tokens are random.
Why does a session end a set time after it opens, however much it's used?
It limits how long a stolen session can be used. The end is kept in the database, where the browser can't move it.
Why does someone else's conversation get a 404, rather than a 403?
A 403 would say that a conversation with that id exists. A 404 tells them nothing they didn't know, as HTTP allows when a server hides that a resource exists.
Why is the owner in the query that finds the conversation, rather than compared after?
Whatever the query gives back is already the person's, so no route can forget the comparison.
What does an index on (owner, started_at DESC, id) do for a person's newest conversations?
It finds that person's conversations in the order the list wants, so the first few come straight from the index, with no sort over the rest.
Which paths back may a sign-in send a person to?
Only a path on this site: one that starts with a single /, not // or /\, with no tabs or line breaks. Anything else goes to /.
Why does every sign-in get a new session token, even when the browser already has one?
Someone may have put that token in the browser before the sign-in. Signed in, it would let them in as the person who signed in.
What must signing out do besides deleting the browser's cookie?
End the session in the database, so that a copy of its token signs no one in.
Why are the desk's cookies SameSite=Lax, rather than Strict?
The provider sends the browser back to the desk from another site. A Strict cookie isn't sent with that navigation, so the callback would never see the sign-in it finishes.
Which requests does the desk refuse as coming from another site?
Any whose method isn't GET, HEAD or OPTIONS, when its Sec-Fetch-Site isn't same-origin, or, without that header, when its Origin isn't the desk's own address.
What does SecretStr do for the client secret?
It shows asterisks in the settings' repr and JSON, so the secret doesn't end up in logs or tracebacks, and the code that needs it asks for it with get_secret_value().
Does fetch reject its promise when the server answers 404 or 503?
No. It fulfills with the response, and rejects only on errors such as the network failing, so the code checks response.ok and throws for an answer that isn't a success.
In RFC 9457's problem details, what's the difference between title and detail?
title is a short summary of the problem's type, the same each time it happens. detail explains this occurrence, written to help the client correct it.
Why does this.#fetch(url) throw in a browser, when #fetch holds the browser's fetch?
fetch gets the object as its this, which isn't a window, and Web IDL has a call with the wrong this throw a TypeError. Called on its own, fetch gets the window.
Why can't the chat page use EventSource for the desk's reply?
EventSource takes only an address and withCredentials, so it can't post the caller's message. The page posts with fetch and reads the event stream from its body.
Reading an event stream chunk by chunk, why can't a CR at the end of a chunk end its line at once?
It may be the first half of a CRLF whose LF comes in the next chunk. Ending the line at the CR would make the LF a blank line, which dispatches an event.
What does TextDecoder.decode(chunk, { stream: true }) do that decoding each chunk on its own doesn't?
It keeps the first bytes of a character that the chunk cuts, and finishes the character with the next chunk, rather than giving replacement characters.
In an event stream, what happens to an event the stream ends in the middle of?
It isn't dispatched: only a blank line gives an event, so a reader that stops at the end drops what it has.
Why does the chat put messages on the page with textContent, and never innerHTML?
A message can carry whatever a caller typed or a model wrote. textContent makes a text node and parses no markup, so nothing in a message can run as the page's code.
What does aria-busy="true" on the chat's log do while a reply comes?
It asks assistive technology to wait until the changes are complete, so a screen reader reads the whole reply once rather than every piece as it arrives.
Why does a failed turn take its messages off the page?
The desk rolls back a turn that doesn't finish, so the page shows what the desk has, and the message goes back in the box to send again.
The desk's page sends a Content-Security-Policy that blocks inline scripts. Why does the chat still avoid innerHTML for replies?
The policy is a second defence, not the first: OWASP lists relying on CSP alone among the approaches to avoid. Text that never becomes markup can't run at all.
Why does writing each piece of a streamed reply into a status region hurt screen reader users?
The status is a live region, so each change is read out: the reply is read over and over as it grows, a piece at a time.
Why does stopping a reply with a flag the loop checks leave the request running?
The loop reads the flag only when the next event arrives. While the desk sends nothing, the request, and the desk's place for it, go on. Aborting the request ends it at once.
new Date('2026-10-22') is midnight UTC. Why does the dashboard format it with timeZone: 'UTC'?
Formatted in the machine's time zone, midnight UTC is still the day before anywhere west of UTC, so the row would show the wrong day.
Why does the dashboard's total row leave the median empty, when it sums the other columns?
The days' medians don't give the median of all their turns, which depends on how many turns each day had and how long each took. It would need the turns themselves.
In a problem details object whose type is about:blank, what should the title be?
The status code's phrase, such as Not Found for 404, since the type adds nothing to the status.
Why does POST /conversations answer 201 with a Location header?
Without one, the client takes the new resource to be at the request's own address, /conversations, which is the collection, not the conversation.
What does the desk send a caller who arrives when every place is taken and the line is full?
A 503, at once, with Retry-After: the seconds a running reply has, after which every reply running now has ended.
Why doesn't httpx's read timeout limit how long a streamed reply takes?
It limits the wait for each chunk, so a model that keeps sending pieces never trips it; a limit on the whole reply needs one deadline for the stream.
Why do the 404, 409 and 503 of a streamed reply come from a dependency, not from the route that streams?
A route that yields answers 200 whatever its body does; only a dependency runs before the response is made.
Why does the desk keep nothing of a reply that didn't finish?
Half a reply isn't something the desk said: kept, the model would read it on the next turn. Without it, the caller can send their message again.
Where should a request take a place at the gate, and where should it give the place back?
Take it last, after every check that can refuse the request, and give it back in a finally, so every path returns it.
Why does one leaked place at the gate matter more than one failed request?
It fails every request after it: once the places are gone, the desk replies to no one.
Why does the desk's app make its client to the model in the lifespan, not in each request?
One client keeps its connections to the model open from one request to the next, and is closed once, as the app stops.
Why does the client to the model wait for each read as long as a reply's limit?
httpx's default gives up after a few seconds without data, and a model can take longer than that before it sends anything; the gate's deadline ends a slow reply instead.
What does the cosine similarity of two embeddings measure?
How far they point the same way: the cosine of the angle between them, whatever their lengths.
Why normalize embeddings cut to their first numbers before comparing them?
Cutting shortens each vector by a different amount. Normalized, they have length 1 again, and their dot product is their cosine.
How do you get every query's cosine similarity with every product at once?
Normalize both, then take the matrix product of the queries with the products transposed.
What are a query's nearest neighbours in an embedding search?
The products whose embeddings have the highest cosine similarity with the query's.
How do you rank products by similarity with ties kept in their order?
Sort the negated similarities with a stable sort, which keeps tied values in the order they come.
How does cosine distance relate to cosine similarity?
Cosine distance is 1 minus the cosine similarity, so the nearest products are the most similar.
What does recall at k measure?
The share of a query's relevant products that are among the first k it ranks.
What is the reciprocal rank of a ranking?
1 over the rank of its first relevant product, or 0 if none is ranked: MRR is its mean over the queries.
What does nDCG divide the discounted cumulative gain by?
The DCG of the best ranking there is, every product sorted by its gain, so that nDCG runs from 0 to 1.
What does np.argmax give for a boolean array with no true value?
0, the same as when the first value is true, so a miss reads as a hit unless you check for one.
Why must nDCG's best DCG come from all of a query's products, not the ones shown?
The best order of what was shown says nothing about what the search missed, so the score would reward a search for ranking its own results well.
What should recall at k divide by?
All of the query's relevant products. Dividing by k keeps a query with fewer relevant products than k from ever scoring 1.
How does an inverted file index speed up an embedding search?
It groups the products into lists around k-means centroids, and a query compares itself only with the products in the lists whose centroids are nearest to it.
What does searching more lists of an inverted file index trade?
More of the full search's results found, for more products compared with each query.
How do you measure an approximate search against the full one?
By the share of the full search's first k results it finds, and by how much of the catalogue it compares each query with.
Why subtract the largest logit before taking a softmax?
It leaves every probability as it was and keeps every power at most 1, so nothing overflows.
What probability does softmax give a logit of minus infinity?
None at all: the token is removed.
How do you get a token's probability from its logit and the log-sum-exp of every logit?
Take e to the power of the logit minus the log-sum-exp.
What does a temperature below 1 do to the probabilities?
It sharpens them: dividing the logits by the temperature moves probability to the likeliest tokens.
Which tokens does top-p keep?
The fewest of the likeliest tokens whose probabilities add up to p or more, so the token that reaches p stays.
In what order does transformers apply temperature, top-k and top-p?
Temperature, then top-k, then top-p, which reads the probabilities after temperature.
How does inverse transform sampling draw a token?
It draws u uniformly from 0 up to 1, and takes the first token whose running total of probabilities is greater than u.
Why is a token with probability 0 never drawn by inverse transform?
The running total doesn't grow at it, so no u lands on it: the first total greater than u is always another token's.
How likely are two draws from the same probabilities to differ?
One minus the sum of the squared probabilities.
Why does dividing probabilities by the temperature change nothing?
Every probability is divided by the same number, and sharing them out again so they add up to 1 undoes it. Temperature divides the logits.
What goes wrong when a sampler makes a new generator with the same seed for each item?
Every item draws the same random number, so the draws aren't independent: each item's result is fixed by that one number.
How do you check that a sampler draws each token with its probability?
Draw many times from known probabilities with one generator, and compare how often each token comes up with its probability.
What is fitting a temperature and a shift to a judge's Yes and No logits?
A logistic regression on the margin between the two logits: one over the temperature is its slope, and the shift its intercept.
What does a fitted temperature well above 1 say about a judge?
That it is too sure of itself: its probabilities need pulling towards a half.
How close to the number of positives should calibrated probabilities add up?
Within about twice the standard deviation of the count they predict, the square root of the sum of p(1 − p).
How does a byte-pair encoding learn its vocabulary?
From the 256 bytes, it merges the commonest pair of neighbouring tokens into a new token, again and again, each new token ranked after the last.
Why is text split into pieces before a byte-pair encoding learns its merges?
So that merges stay within a word, a number or a run of punctuation, and no token runs from one word into the next.
Why does a byte-pair encoding start from the 256 bytes?
So that it can encode any text in any language: whatever no merge covers stays as single bytes.
In what order does a byte-pair encoding merge when it encodes text?
Within each piece, it merges the neighbouring pair whose token has the lowest rank first, so the merges come in the order training made them.
Why are a list of tokens decoded all at once, not token by token?
A character's bytes can be split across tokens. Joined first, the bytes read as whole characters; one token's bytes alone can be part of one.
How many bytes does a character take in UTF-8?
One for ASCII and two to four for the rest. Most Japanese characters take three.
Why does a Japanese title take more tokens than an English one with a tokenizer learnt from English text?
Most of its characters take three bytes in UTF-8, and merges learnt from English join few of them: the shop's Japanese titles take 2.1 tokens a character.
What do more merges do to the tokens a text takes?
Each merge joins a pair into one token, so text like the training text takes fewer tokens. Text unlike it gains little.
Which tokenizer should count a text's tokens for a model's limit?
The model's own: the same text takes different numbers of tokens with different tokenizers.
Why not estimate a text's tokens from its characters?
Characters per token depend on the language and the tokenizer: a Japanese title takes 8.5 times the tokens that four characters a token would give with the shop's tokenizer. Count with the tokenizer itself.
How should text from users go into a prompt that uses special tokens?
Encoded as ordinary text, so that text spelling a special token stays text. Only the code puts special tokens in.
How do you show a reply's text as its tokens stream in?
With an incremental decoder, which holds back the bytes of a character until its last byte arrives.
Why can a text's tokens be counted piece by piece?
No token crosses from one piece into the next, so a text's tokens are its pieces' tokens, one piece after another.
Can a longer start of a word take fewer tokens than a shorter one?
Yes: one more character can let merges join what they couldn't before. With the shop's tokenizer, Frui takes 4 tokens and Fruit takes 3.
Why shorten text before a model cuts it at its limit?
A cut at the limit falls wherever the tokens run out, inside a word or a character. Shortening first keeps whole pieces.
A streamed reply turns out to cite nothing. What becomes of the pieces the student has already seen?
The verdict that ends the stream takes their place: the question goes to staff, and the pieces were only ever the reply on its way.
Why does a reply that stopped at the token limit go to staff?
It can end mid-sentence after a citation, so its pages can't vouch for what it says; only a reply that stopped by itself is checked as an answer.
Why does the desk find the student by the office's directory rather than by an id in the request?
Anyone can put any id in a request; the session names a person the college signed in, and the office's own directory says which student they are.
What bounds how many of the desk's replies run at once?
Each reply holds a pool connection from question to verdict, so the pool's size: a question past it waits for a connection to come back.
Why give a desk's latency as a percentile rather than a mean?
A percentile says what most students wait, such as 95% of them; a mean can look fine while the slowest replies keep students waiting.
What does it mean for a server to be busy for a share near 1 of its busiest hour?
It's answering all hour, so a question that comes while it's busy waits for the ones ahead of it, on top of its own reply.
Why can't a field in the request say which student's aid to show?
The caller writes the request; only the session the provider vouched for, and the office's directory, say who the caller is and which student they are.
What should a service's log keep about the questions its users ask?
What happened, such as what a question was about and how it ended, never its text or who asked it, since logs travel further than the database.
Why does a write-up give a change between two releases with its margin?
Without the margin, the customer can't tell a change the release makes from one the questions happened to give.
Why can the faster release be the wrong one to run?
Speed is one figure: three passages cut the prompt but raised the share of answers citing the wrong pages by more than the margin, and a wrong answer costs a student more than a slower one.
Why does the office's assistant pass a reply that cites no passage to staff?
A student can't check an answer against pages it doesn't name, and a reply without a citation isn't held to the office's pages.
Why does a question the search turns away never reach the model?
The pages don't answer it, so an answer could only come from elsewhere; going straight to staff costs no tokens and gives no made-up answer.
Why does the desk read a student's record as a role the policies hold, rather than as the tables' owner?
Superusers, roles with BYPASSRLS and, normally, a table's owner bypass row-level security; only a role the policies hold reads just the student set.
Why SET LOCAL ROLE rather than SET ROLE on a connection kept open?
SET LOCAL lasts until the transaction ends; a SET in a transaction that commits stays for the rest of the session, and every later query runs as that role.
Why compare two releases question by question rather than by their scores alone?
Both answered the same questions, so each question's difference takes out what the questions share, and the margin is narrower than two scores compared alone.
Why count a page's questions together in an eval's margin of error?
Questions about one page share its passages, so their outcomes move together; counted apart, they make the margin too narrow.
Why does a student's id go to Postgres as a parameter, even when it comes from sign-in?
A parameter is a value whatever it holds; text written into the query with an f-string can end its string and run as SQL.
A message numbers its passages from [1]. Which passage does a reply's [n] name?
The passage at place n - 1; a number beyond the passages, or 0, names none and is left out.
Why does the desk give a student's aid from their record rather than ask the model?
The record is the answer, read for the student signed in; a model that reworded it could get a number wrong.
Why does a question for staff from a visitor who isn't signed in write no ticket?
Staff reply by email to the student signed in, so there's no one to reply to; the desk asks the visitor to sign in instead.
Why keep a digest of each passage's text beside its vector?
It tells a sync which texts have changed since they were embedded, so it embeds only those, and a passage whose text is the same keeps the vector it has.
Why embed every changed text before changing the table?
A request that fails then fails before any write, and with the writes in one transaction, the table is either brought up to date or left as it was.
Why fuse a search's two rankings by their ranks rather than their scores?
Cosine distances and ts_rank's values aren't on one scale, and ranks are: each passage scores the weight over k plus its rank in each ranking that found it.
Why does a query that keeps each page's best passage with DISTINCT ON need an outer query to give the pages best first?
Postgres needs the DISTINCT ON expressions to match the leftmost ORDER BY ones, so its rows come in page order, and only an outer ORDER BY can sort them by score.
Why turn away a question whose nearest passage isn't close enough to it?
A search always gives the nearest passages there are, even for a question the pages don't cover, and an answer from them would be wrong.
Why give a customer a threshold's rates on questions other than the ones it was chosen on?
Chosen to keep a share of those questions, it keeps that share of them by construction; questions that had no say in it show what it will do.
Why does a search by the words of a typed question join them with | rather than &?
Few passages hold every word of a sentence. With |, any of its words match, and ts_rank puts the passages that hold more of them first.
Which httpx exception covers both a reply with an error status and a request that failed?
httpx.HTTPError: HTTPStatusError, from raise_for_status, and RequestError, for timeouts and dropped connections, both come from it.
Why does a search service set a timeout on each call it makes to its model?
A request with no timeout waits for ever on a model that never answers, and the student's question waits with it.
Why does the office's search go by words alone when the model can't embed a question?
The words still find pages for nearly every question, and a student is better served by those than by an error.
Why build a word search's index once, before any question?
Kept for each word, the passages that hold it let a question score only the passages that share one of its words, instead of reading every passage again.
Why does a support assistant read the pages it cites from the passages' numbers rather than the model's words?
The numbers name passages the code gave the model, so each cited page is one the answer was given from. A page the model names in its words could be one it made up.
How do you check that a binary tree is a search tree?
Walk it in order, left subtree, node, then right subtree, and check that each value is greater than the one before. Checking each node against its children alone misses values out of place further down.
With bricks and ladders to climb a row of buildings, which steps should the ladders take?
The highest steps so far. A min-heap holds the ladders' steps, and when it holds one too many, its smallest goes to bricks.
A trip plan is several bookings. How do you book them?
Hold every part, then book them all, and release the holds if any part fails, so the customer never has half a trip.
When scoping from a customer's brief, what measure of success do you write down?
The customer's own, in their numbers, such as the hours officers spend a week, not a measure of the tool.
Why does scaled dot-product attention divide its scores by ?
A dot product of entries of variance 1 has variance . Dividing by brings the scores back to a spread of 1, so the softmax doesn't put nearly all its weight on one key, where its gradient vanishes.
How does a causal mask hide a later token from a query?
Its score becomes before the softmax, so its weight is exactly 0 and the other weights still add up to 1.
What does True mean in a mask of booleans that F.scaled_dot_product_attention takes?
That the query may see the key: it takes part in attention.
Why does a softmax subtract each row's largest score first?
It changes no weight, and it keeps exp from overflowing, past 709.8 in float64 and 88.7 in float32.
How many parameters does MultiHeadAttention(width, heads) hold, and how does the number of heads change it?
, whatever the number of heads: the heads are slices of the same four layers.
How do you split (batch, length, width) into heads for attention?
.view(batch, length, heads, width // heads).transpose(1, 2), giving (batch, heads, length, width / heads).
Past what length do attention's scores and values cost more than its four layers?
At : the layers take multiply-adds and the scores and values .
What does True mean in a mask of booleans that nn.MultiheadAttention takes?
That the query may not see the key: the opposite of F.scaled_dot_product_attention's mask.
Why does writing pad stories before them, rather than after?
So that every story's last token, from which the next one is predicted, sits at the batch's last position.
How does a padded query that sees no key spread nan through a story?
Every score it has is , so its softmax is nan. The next layer makes keys and values from its output, and 0 times nan is nan, so every token's weighted sum picks it up.
What should attention give a query that sees no key?
Weights of 0 and an output of 0, as F.scaled_dot_product_attention gives.
Why can one mask (batch, 1, length, length) serve every head?
Every head of a story hides the same keys, so the 1 broadcasts over the heads.
What does F.dropout(x, p) do in a model put in use with .eval()?
It still drops: its own training argument defaults to True. Pass training=self.training, or use nn.Dropout, which follows its module's mode.
What does tril(diagonal=1) keep that a causal mask mustn't?
The diagonal above the main one, so each token sees the next token: the one it is trained to predict.
In multi-head attention, the square root of what divides the scores?
Of a head's size, width / heads, since each head's scores are dot products of its own slice.
What three values does a running softmax keep for each query?
Its largest score so far , the sum of its exponentials less , and the sum of the values weighted by them. The output is .
When a tile brings a larger score , what happens to the running sums?
Both are multiplied by , which puts the old terms on the new footing, and the tile's own terms are added.
How does attention in tiles change its memory and its work?
Its memory grows with the story's length rather than its square. Its work stays the same: every query against every key.
How does a safetensors file say where each tensor's numbers are?
Its header gives each tensor's dtype, shape and data_offsets, [begin, end], counted from the first byte after the header.
Why can loading a safetensors file run no code, when loading a checkpoint saved with torch.save can?
A safetensors file is a JSON header and raw numbers. A torch.save checkpoint is a pickle, which can name code to run as it loads.
What are the attn.bias tensors in GPT-2's weights file?
Each block's causal mask, ones on and below the diagonal. They aren't parameters, and a GPT that makes its mask as it runs needs none of them.
How do GPT-2's vocabulary files write bytes as text?
Each byte has a character: the bytes that print as themselves in Latin-1 keep theirs, and the others take characters from U+0100 on, so a space is Ġ.
In GPT-2's tokenizer, which pair of strings in a piece is joined next?
Of the pairs of neighbouring strings, the one listed first in merges.txt, wherever it stands. Joining stops when no pair left is a merge.
Why does decoding join all the tokens' bytes before turning them into text?
One character can take more than one token, so decoding each token alone would cut it in pieces, each shown as U+FFFD.
Why must GPT-2's linear weights be turned around to go into nn.Linear?
GPT-2's code multiplies x @ weight, so it keeps a weight as (inputs, outputs), and nn.Linear keeps (outputs, inputs).
Why can a weight copied without turning it around go unnoticed?
Where a layer's inputs and outputs are the same size, it fits, and the model runs with other numbers. Only a check against the model's own outputs finds it.
Why does taking the likeliest token every time make poor stories?
It often makes the model repeat itself. Drawing each token from its chances, as write does, lets what it writes vary.
Why isn't reshaping a weight to its turned-around shape the same as transposing it?
Reshaping keeps the numbers in their order and only regroups them, so each new row mixes pieces of the old rows. Transposing makes each row of the new weight a column of the old.
What does ending a prompt with GPT-2's separator do?
GPT-2 reads the prompt as a document that has ended, and starts a new one instead of going on with it.
Why do mistakes in loading a model's weights so often go unnoticed?
They raise no error: shapes still fit and the model still gives numbers. Only comparing with the model's known outputs, or its tensors with the file's, finds them.
Why can't a loader read GPT-2's number of heads from its weights file?
The heads split each block's width without a parameter of their own, so every tensor has the same shape whatever their number. Each of GPT-2's sizes has heads 64 wide.
What must the tensors of a safetensors file do with the bytes after its header?
Cover every one of them once: each tensor's bytes fit its dtype and shape, with no overlap and no gap, and the last ends where the file does.
Why check a weights file against its header, when a broken file often raises an error anyway?
Not every one does: two tensors on the same bytes, or a gap between tensors, load without an error.
Why does a score between a rotated query and a rotated key depend only on how far apart their tokens are?
Each pair of the query turns through and of the key through , so in their dot product each pair meets at .
What angle does pair of a head wide turn through at position ?
, with . SmolLM2's base is 100,000.
Which numbers does SmolLM2 pair when it rotates a head's query?
Each number with the one half a head away, with , as the Llama code in transformers does. The rotary paper paired neighbours, and a model's weights work with one pairing only.
What does RMSNorm leave out of layer normalization?
The mean and the bias: it divides by the root mean square and multiplies by a learnt weight, nothing else.
In grouped-query attention with 9 query heads and 3 key and value heads, which key head does a query head read?
Its group's: each key and value head serves 3 neighbouring query heads, which repeat_interleave lines up.
Why is SmolLM2's SwiGLU MLP 1,536 wide, not four times its width?
SwiGLU has three matrices, so two thirds of four times the width keeps its parameters those of a two-matrix MLP four times as wide.
Why can a KV cache keep the keys and values of the tokens already read?
No token reads a later one, so the keys and values of the earlier tokens never change as the story grows.
Why does a KV cache keep keys after rotating them?
A key's angle depends only on its own position, which never changes, so it's turned once, when it's made.
How many bytes does SmolLM2's cache hold for each token in float32?
46,080: a key and a value of 3 heads, 64 wide, in each of its 30 blocks.
Why must a cached step's new tokens carry their positions on from the cache's length?
The cached keys were turned by their own positions, so a new token turned as if at position 0 reads them as if it stood at the start of the story.
Why does PyTorch's is_causal=True go wrong with a KV cache?
With fewer queries than keys, it lines its triangle up with the first keys, so each new token reads the start of the story instead of the whole cache.
With 9 query heads in groups of 3, why repeat the key heads with repeat_interleave, not repeat?
repeat_interleave repeats each key head in place, so each group of neighbouring query heads reads its own key head. repeat lays whole sets one after another.
Why pad a batch of prompts on the left, not the right?
Every prompt's last token lands in the last column, so each story's next token comes from the same row of logits, and the tokens written after it line up.
Where do the positions of a story padded on the left start?
At its own first token, as they would alone: each place less the row's padding.
What does writing stories in a batch save, with a KV cache?
Passes, not multiply-adds: one pass a step reads the model's weights once for every story in the batch.
Over what does layer normalization take its mean and variance?
Over each token's own vector, its last dimension, so it does the same in training and in use, whatever else is in the batch.
Does layer normalization's variance divide by or , and where does go?
By , and goes inside the square root: .
What can layer normalization's weight and bias do that the normalization can't?
Undo it: a weight of and a bias of give the vector back.
What does a GPT-2 block do to the residual stream ?
, then .
Where do a pre-norm block's layer normalizations sit, and what do they leave alone?
At each part's input, inside its branch. The residual stream itself is never normalized.
How wide is a GPT-2 MLP's hidden layer, and what share of a block's parameters does the MLP hold?
Four times the width, so it holds of the block's : about two thirds.
What is GPT-2's output layer?
Its token embedding table, tied: a token's logit is the dot product of the final vector with that token's own embedding.
Why can't GPT-2 read a story longer than its context?
Its position embeddings are learnt, one vector for each place in the context, so it has no vector for a later position.
Why does GPT-2 scale the starting weights of the layers that write to the residual stream by ?
The stream adds random outputs, so their variances add up. Dividing each one's variance by keeps the stream's size the same at any depth.
How many parameters does GPT-2 small's tied output layer save?
A matrix of the vocabulary by the width: 38,597,376, 31% of the model.
How do you tie an output layer to a token embedding table in PyTorch?
Assign the parameter: head.weight = wte.weight. Copying its numbers makes a second parameter that drifts once training starts.
Where did the original transformer put its layer normalizations, and where does GPT-2?
The original after each addition, normalizing the residual stream itself. GPT-2 at each part's input, leaving the stream alone.
What goes wrong when a GPT's positions start at 1?
Position 0's vector is never used, and a story as long as the context asks for a position past the table's end.
How many parameters does a GPT-2 block of width hold?
: attention's , the MLP's , and two layer normalizations of .
How many multiply-adds does a GPT take for each token of a forward pass?
: about one for each parameter of the blocks and the output layer, plus attention's, which grow with the story's length .
Why does training take about three times a forward pass's multiply-adds?
Each matrix multiply's backward pass takes two of its size, one for each input's gradient.
Why does the app's tokenizer give <|endoftext|> a token of its own?
So a story's end is one token the model can learn to write and the app can stop on, and text in a story can never spell it.
What does doubling a GPT's vocabulary cost, and what does it buy?
It doubles the token embedding, which is the output layer too, and makes each token only a little longer, so a story takes a few fewer tokens.
How can training byte-pair merges avoid counting every pair again after each merge?
Keep each pair's count and the pieces it's in. A merge then updates only the pieces it changes: their old pairs come off the counts, their new ones go on.
How did GPT-3 put its documents into its context windows?
One after another, each followed by an end-of-text token, in windows of the full context that run across the separators, with nothing masked.
What loss does a GPT start near, with a vocabulary of tokens, and why?
, the loss of guessing every token as likely as any other: its first logits are all close to 0.
Why hold back whole stories to validate with, rather than windows of one stream?
Windows of one stream would put parts of the same story in training and in validation, and a model scores well on a story it has partly read.
In GPT-style training, which parameters take weight decay?
The matrices: the embeddings and the linear layers' weights. The biases and the layer normalizations' weights take none.
What did GPT-3's learning rate do over its run?
It rose in a straight line over a warm-up, then fell along a cosine to a tenth of its peak.
Writing past its context, what does a GPT read?
Only the last context's worth of tokens: the story so far, cut from the front.
What does a GPT learn when each target is the token it's reading?
To copy: causal attention lets each position see its own token, so the loss falls fast while the model learns nothing of the next token.
In a cosine schedule from the peak down to a floor, what multiplies ?
, which runs from 1 to 0 as runs from 0 to 1. The cosine alone runs down to −1, and takes the rate below zero.
Writing a story longer than the context, which tokens should the model read?
The latest ones, tokens[-context:].
For a fixed budget of compute, how should a language model's size and its training tokens grow, by Chinchilla's results?
Together: for every doubling of the model's size, double its training tokens.
What does training a GPT cost for each token it reads?
About three times a forward pass of one token: multiply-adds.
With the compute fixed, what does making a GPT smaller buy?
More tokens read: a run's multiply-adds are its tokens times its cost a token.
A demo must keep a customer's personal numbers from a model. Where in the code are they removed?
Before the request is built, so nothing after it, a retry or a log of what was sent, can carry the number.
What should a demo built on a model do when the model is down, slow or answers in a way it can't read?
Fall back to what the customer does today, such as a person's queue, after a timeout and at most one more try, so nothing is lost and nothing waits forever.
A model answers from a customer's own answers and cites them by id. What is its reply checked against before a caller sees it?
The ids it was shown: a reply that cites none of them, or one it wasn't shown, gets the customer's handover instead.
How does a demo against a written brief show that it meets it?
Each of the brief's criteria is a test, planned before the code, so every promise has run before the demo.
A task is estimated at 3, 5 and 12 days. Why is its mean more than its likely 5 days?
The triangle's long side runs towards 12, and the mean, the average of its three corners, lies on that side of the peak.
Why is a project's average finish later than the finish you get from every task's average days?
Where paths meet, the project waits for the slowest. A delay on any path delays it, while time saved on one is lost waiting for the others.
In what order does Python's random.triangular take its arguments?
triangular(low, high, mode): the mode comes last.
Every task is given its own 80% days, and they're added along the paths. Is the result an 80% promise for the project?
No, it's later than one: it needs every task to run late at once, which is rarer than one run in five. The project's percentiles come from its own simulated runs.
A simulation makes a new random.Random(seed) for each task, with the same seed. What happens to its runs?
Every task draws the same numbers, so in each run all the tasks fall at the same point of their distributions and run late or early together. One generator for the whole simulation keeps the tasks apart.
Four runs in five finish within 28.06 days. How many whole days can be promised?
29: the first whole day by which four runs in five have finished. Rounding down to 28 promises a day fewer runs meet.
A price is split into milestone payments. How do they add up to the price exactly?
In whole cents: each payment is its share rounded down, and the cents left over go one each to the milestones with the largest remainders.
What does Python's date.weekday() return for a Saturday?
5: it counts Monday as 0 and Sunday as 6, so the working days are those below 5.
A search must return only documents the person may read. Why filter before taking the best k, not after?
Filtering after gives fewer than k, or none, when the best matches are for others. Ranked within what they may read, the k places all go to documents they can open.
A document names no group that may read it. Who should?
No one: access fails closed, so a document whose groups were never set stays unread until someone gives it some.
When may an app believe the groups in an ID token?
Once the token checks out: signed by the provider's key, for this app, by the provider's issuer and not expired. Before that, the claim is only what the caller sent.
A directory gives someone a group the app doesn't know. What access should it give?
None: the app maps the directory's groups it lists to its own, and leaves out the rest.
Why does an audit log name people by a keyed HMAC of their id, not a plain hash of it?
Anyone with the list of ids could hash each one and match a plain hash. A keyed one stays the same for each person, and only whoever holds the key can link it back to them.
jwt.decode(token, options={"verify_signature": False}) reads a groups claim. What can a caller do?
Write a token with any groups and sign it with their own key, or none: unchecked, its claims are only what the caller sent.
Why check an allowed host in an httpx transport rather than where the code builds its addresses?
The transport sees every request, redirects and addresses from replies or documents included, with the host as httpx parsed it.
An allowed list holds model.office.test. Why isn't host.endswith(name) enough?
evil-model.office.test ends with it too. A host is allowed by its whole name, compared in lower case.
An agent is run five times on each of 20 tasks. Why is its standard error taken over the tasks, not the 100 trials?
A task's trials share its wording and difficulty, so they aren't separate samples of the contacts to come. Each task's share counts once, and the error is the spread between tasks over the square root of their number.
Every trial of a kind of task was done, so its standard error is 0. What does that show?
Only that its tasks never failed in these trials. No spread was seen to measure, so it can't show that the agent never will.
An agent does 70% of a test set's tasks. Why can it do far fewer of a customer's contacts?
The share depends on the mix of kinds. Weighted by the customer's own contacts, the kinds it does least often can weigh far more than they do in the test set.
What is the standard error of a weighted sum of separate shares, with weights and errors ?
: the variances add, each scaled by its weight squared.
The desk saves $8,015 to $11,767 a month, and the pilot costs $40,600. What range of months does it take to pay for itself?
3.5 to 5.1: the most a month pays back soonest, so the months' low end comes from the saving's high end, and their high end from its low end.
Why does a fixed monthly cost move a net saving's estimate but leave its interval as wide?
It's the same in every case, so it shifts the estimate without adding to its standard error, which comes only from the shares' errors.
A report gives the share of a customer's contacts an agent does as the share of all its test trials done. What's wrong with it?
It weights each kind of contact by its trials in the test set. The customer's share weights each kind by the customer's own contacts.
Why are an interval's ends rounded outward in a report for a customer?
So the interval shown always holds the one worked out. Rounding to the nearest can move an end inward and claim more certainty than the data gives.
Why count only the values present when checking a column's allowed values or range?
isin and between are False for a missing value, so a missing value would count twice: as missing, and as a wrong value.
How do you test a data contract before relying on it?
Run it on data you know to be good. Whatever it reports there is a mistake in the contract, such as the documentation's spelling of a value.
What should a failed data check report?
The column, the rule broken and how many rows break it, so the failure says where to look.
How do you mark every row of a value that repeats, the first one included?
duplicated(keep=False). By default duplicated leaves the first row of each value unmarked.
Why check a lookup table's codes as well as the data's?
A code listed twice doubles every row it describes in a join, so the lookup table has a key to keep too.
What should a check do with a code the lookup table lacks?
Report how many rows have it, rather than fold them into unknown: it may be a new code the table hasn't caught up with.
How do you flag the rows of each group that come after a row with some property?
Sort, then take the row's own flag off its group's running count: flag.groupby(group).cumsum() - flag > 0.
How do you compare ordered categories, such as age groups?
By their place in a list of the categories in order: map each to its rank, then compare the ranks.
What should a check do with rows it can show contradict each other but can't correct?
Hold them back and name them, for someone who knows the records to decide which value is wrong.
Why is values.isin(allowed).any() the wrong test of a column's values?
It's true when a single value is allowed, so it passes a column with one value wrong. .all() stops it.
Why does (x < low) | (x > high) let a missing value through?
A comparison with a missing value is false. ~x.between(low, high) is true for it.
How should a data check be tested?
On the smallest break it must stop, one at a time, and on data you know to be good.
Why compare two batches of data by shares rather than counts?
Batches differ in size, and counts change with the size alone. Each value's share of its batch's rows compares across sizes.
Comparing two batches, how do you keep a value only one of them holds?
Line their shares up with an outer join, such as pd.concat(..., axis=1), and fill the side that lacks the value with fillna(0).
Why does comparing shares need value_counts(dropna=False)?
Without it, value_counts leaves missing values out, so a column going missing never reports the missing value's share.
With na_values=["?"] and keep_default_na=False, which strings does read_csv read as missing?
Only ?.
Why keep None in A1Cresult as a value, rather than read it as missing?
It records that the test wasn't done, which is information, not a gap in the record.
What does table.isna().mean() give?
The share of each column's values that are missing.
Which pandas type holds whole numbers and missing values?
Int64, with a capital I. A missing value in an int64 column turns it into float64.
Why read a code such as NULL or Not Mapped as missing?
It records that there was no answer. Left as a code, it counts as a category of its own.
What does dropna() do to a table with a column that's mostly missing?
It drops nearly every row, since it keeps only the rows with no missing value.
How do you keep each patient's earliest encounter in pandas?
Sort by encounter_id, then drop_duplicates("patient_nbr"), which keeps the first row of each patient.
Why measure a readmission rate over each patient's first encounter, not every encounter?
Patients who come back often count once for every encounter, which inflates the rate.
What does isin give for a missing value?
False, so a filter on ~isin(codes) keeps it.
What does value_counts(normalize=True) do with missing values?
It skips them, so each share is out of the values present, not out of every row.
drop_duplicates("patient_nbr") keeps which of a patient's rows?
The first it meets in the table's order, whatever that order is.
How do you test that a cleaning step doesn't depend on the order of its input?
Clean the input in another order, such as shuffled rows, and compare the results.
How do you find each patient's earliest encounter when the rows come in chunks?
Keep each patient's earliest encounter so far. Join each chunk to it with pd.concat, and keep the earliest of each patient again with first_encounters.
Why can't deaths and hospice care be left out chunk by chunk?
Whether an encounter is a patient's first is known only once every chunk has been read. Leaving one out early counts the patient at a later encounter.
Why can a column read in chunks come out with different types?
read_csv works out each chunk's types from that chunk alone. Giving it dtype makes every chunk read alike.
How do you read CSV text held in a string with pandas?
pd.read_csv(io.StringIO(text)): io.StringIO makes the string act as a file.
Why read the ID mapping with keep_default_na=False?
By default read_csv reads NULL as missing. With it, NULL stays the description the file gives.
Why trim the spaces around text values before joining or grouping on them?
A value with a space before it is a different value, so one group would split in two.
Which join keeps every encounter when the lookup table lacks a code?
A left join, how="left". An inner join, merge's default, drops the encounter.
What does validate="many_to_one" do in a merge?
It raises MergeError if a key appears more than once on the right, which would repeat rows on the left.
How do you find the rows a merge couldn't match?
indicator=True adds a column _merge, which is "left_only" for them.
What does groupby do with missing values by default?
It leaves them out. dropna=False keeps them as a group of their own.
How do you get each group's count of rows and of True values in one step?
.agg(encounters="size", readmitted="sum") on the grouped True and False values.
Why leave small groups out of a report of shares?
One encounter more or fewer moves a small group's share a long way, so its share says little.
Why isn't the share for all groups the mean of the groups' shares?
The mean counts a group of one patient as much as a group of thousands. Divide all the readmissions by all the patients.
What does drop_duplicates("encounter_id") drop from a table of encounters?
Nothing, since each encounter has an id of its own. Patients repeat, so keep one row per patient_nbr.
How do you test a report's joins and groupings?
Count a small extract by hand, such as one patient with two encounters and one code the lookup lacks, and compare the report with it.
How do you count, for each row, the rows of its group that came before it?
Sort the rows into order, then groupby(...).cumcount(), which numbers each group's rows in order.
Why can't a model used at discharge count a patient's later encounters?
They aren't known at discharge, and a readmission is itself a later encounter, so they'd hand the model part of what it's meant to predict.
Does merge keep the left table's index?
No. Joining columns on columns, it ignores both indexes and gives the result a new one. map keeps the caller's index.
Why pass values to SQL as ? placeholders rather than writing them into the statement?
The database takes each as a value: a quote can't end the text early, and text from outside can't add SQL of its own.
What happens to rows inserted with sqlite3 if the connection closes before commit()?
They're lost: an INSERT opens a transaction, and only commit() saves it.
How does a table refuse a second row for the same key?
A primary key on the columns that make the key, such as PRIMARY KEY (column_name, code): inserting the same key again raises IntegrityError.
In a LEFT JOIN, why does a condition on the right table belong in ON rather than WHERE?
For a row that matches nothing, the right table's columns are NULL, so a WHERE condition on them drops it, and the left join acts as an inner one.
How do you join a lookup table that holds the codes of several columns?
Once for each code column, under its own name, matching both the column's name and the code.
In what order does SQL return rows without ORDER BY?
In no promised order. Name the order with ORDER BY.
What does SQL's GROUP BY do with NULL?
It keeps the NULLs as one group of their own.
Why does SUM(x) / COUNT(*) give a whole number in SQLite, and what gives the share?
Dividing one whole number by another gives a whole number. AVG(x) gives the share as a fraction.
How do you put a column's name into a query safely, when a ? can't hold one?
Check it against the table's own column names first, and refuse any other.
Why does x NOT IN (SELECT ...) keep no rows when the subquery gives a NULL?
A value is never NOT IN a list that holds a NULL: the condition is NULL, and WHERE drops the row. NOT EXISTS is true when no row matches.
What does WHERE do with a row whose condition is NULL?
It drops it: WHERE keeps only the rows whose condition is true.
Joining each patient to their first encounter, what must the join match?
Both the patient and the encounter. Matching the patient alone brings every encounter of the patient through.
How do you count, in SQL, the rows of each row's group that came before it?
A window function with a frame that stops at the row before: COUNT(*) OVER (PARTITION BY g ORDER BY t ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING).
Which rows does an ordered window's frame hold by default?
From the partition's first row up to and including the current one, so an aggregate counts the current row too.
What does SUM give over no rows?
NULL, where COUNT(*) counts none. COALESCE(SUM(x), 0) replaces it.