Skip to content
Preptima
mediumCodingMidSenior

Write a helper that polls a condition until it is true or a timeout expires, then tell me what yours does when the condition throws.

4 min readUpdated 2026-08-15Target archetype: Big Tech, Enterprise Captive, Product Startup

Answer

Short answer

A correct wait helper tracks a wall-clock deadline rather than an attempt count, treats an exception from the condition as a not-yet rather than a failure, returns the condition's value, and reports the last observed value or error when it gives up.

Was this answer helpful?

Likely follow-ups

  • How would you make this cancellable so shutting a suite down does not wait out every remaining timeout?
  • The condition costs a database query each time. What changes about the polling strategy?
  • How would you test this helper without making the test take thirty seconds?
  • Where is polling the wrong tool entirely, and what replaces it?

Related questions