Interview prompt
Explain branch prediction and exposing parallel work to an engineer who understands the surrounding system but has not used this technique. Walk from its contract to a concrete operation, then discuss where it fails or becomes expensive.
A strong answer
Branches make program control depend on data. A modern CPU predicts likely directions so it can keep fetching instructions before the condition is fully resolved. Correct predictions preserve pipeline flow; incorrect ones require discarding speculative work and refilling parts of the pipeline.
A loop over sorted values may have a highly predictable branch, while a branch on randomized values may be difficult to predict. Some code can be expressed with conditional operations or grouped data to reduce unpredictable control flow, but those rewrites can increase instruction count or complicate correctness.
A complete answer also calls out the assumptions that control correctness. Instruction-level parallelism is limited by dependencies, branches, and memory stalls. Removing a branch is not automatically faster; the new operations may cost more or prevent vectorization. Benchmark realistic input distributions and check that the compiler generated the intended code.
Close by describing one representative test or measurement. You have a filter with a branch that is true for almost every element. Explain why that may perform differently from a branch that is true half the time, and name one measurement you would collect.
Follow-up questions
Answer the follow-ups in the frontmatter. Use the linked article for the concept and the trace to make the explanation concrete.