Question presented to candidate: "If you write config.retries ??= 3 and config.retries is already 0, does it get overwritten? What if you'd used ||= instead β same answer?"
What a strong answer should cover:
- π Interview term:
??=,||=,&&=β the logical assignment operators, each conditionally assigning the right-hand side to a variable/property based on the CURRENT value, combining a logical check with assignment in one step. - π Interview term: the real, direct answer to the prompt β verified directly:
x ??= valuegenuinely only assigns whenxis currentlynull/undefinedβ a real, legitimate0genuinely survives??=untouched.x ||= valuegenuinely assigns for ANY falsy value β the identical real0genuinely gets overwritten by||=, a real, sharp, different answer to the prompt's second question. - π Interview term:
&&=β the mirror case:x &&= valuegenuinely only assigns whenxis currently truthy β verified directly, anullvalue genuinely staysnull, since&&=never assigns to something already falsy. - π Interview term: genuine short-circuiting β verified directly with a real call counter: when the condition for assignment is not met, the right-hand side expression is genuinely never evaluated at all β not just its result discarded, the exact same short-circuiting guarantee this bank's own dedicated short-circuit-evaluation question covers for plain
&&/||. - A precise answer names the real, practical use case directly matching the prompt:
config.retries ??= 3is the correct, idiomatic way to supply a default ONLY for a genuinely missing config value, without accidentally clobbering a deliberately-set0,false, or"".
Clarifying questions expected:
- None β this is a definitional/technical question; directly answering both halves of the prompt's own scenario (
??=vs.||=on a real0) is the strong signal.
Code / implementation expected: Yes β the direct side-by-side ??=-vs-||=-on-a-real-0 comparison is the clearest, most convincing demonstration.