Build Angular signals: signal, computed and effect with versions, glitch-free reads, equal and untracked
Problem
Angular signals are the base of modern Angular: templates, input(), computed() and effect() are all nodes in one reactive graph. Interviewers like to ask how that graph avoids extra work: why a computed is lazy, why it never shows a half-updated value (a "glitch"), and why a computed that recalculates to the same value does not wake up anything downstream. Build the core in plain JavaScript.
const price = signal(100);
const qty = signal(2);
const total = computed(() => price() * qty()); // nothing runs yet
effect(() => console.log("total", total()));
flushEffects(); // logs "total 200"
qty.set(3);
price.set(50);
flushEffects(); // logs "total 150" once, never "total 300"Ref. UR-1450 · © UIRound · uiround.com/questions/build-angular-signals-signal-computed-and-effect-with-versions-glitch-free-reads-equal-and-untracked
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
- 3
- code listings
- 8
- edge cases
- 5
- follow-up questions
What's inside
- The full problem, with examples
- Tests that check your solution
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Card worldwide
- UPI and net banking in India
- One-time payment, nothing renews
Stuck? Take a hint
- 1Split the work into two phases. A write only pushes a dirty flag down the graph (computeds and effects). Nothing is recomputed until someone reads a computed or an effect is flushed.
Try it in the editor
Run your code as often as you like. Submitting it to the full test suite is part of Pro.
Private, and saved in this browser as you type. All your notes are on My lists.