Door in an infinite wall algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The infinite-wall door problem asks how to find one target location when search space has no known boundary. A one-direction walk can fail forever if the target is on the other side. The standard solution is an expanding search pattern that guarantees discovery with bounded overhead.
Problem Model
Assume positions on the wall are integer coordinates and you start at zero. A hidden door exists at unknown coordinate d, which can be positive or negative. Your algorithm can probe positions and ask whether the door is there.
A correct strategy must satisfy two goals:
- Completeness: door is always found eventually.
- Efficiency: travel or probe count grows reasonably with distance.
Why Simple Strategies Fail
Linear one-side scan is incomplete on unbounded two-sided space. Symmetric small-step zig-zag is complete, but inefficient for distant targets because it revisits near positions too often.
Exponential expansion solves this tradeoff by doubling search radius each phase.
Exponential Alternating Search
Probe sequence can be:
+1-2+4-8+16
Distance doubles and direction alternates, so both sides are covered quickly.
This formulation assumes direct probing. If movement cost matters, you can still use the same boundary schedule but account for travel path between probes.
Complexity Intuition
Let absolute door distance be n. Once search radius exceeds n, the door must be inside explored range on one side. Number of phases is proportional to log distance, and total probe operations remain proportional to distance with a constant-factor overhead.
This is much better than linear growth in revisit-heavy patterns.
Practical Variant with Continuous Movement
In robotics or games, movement cost can dominate probe count. You can keep current position and move to each new checkpoint rather than teleporting conceptually. Log both metrics:
- Probe count.
- Total travel distance.
This utility helps test navigation code before integrating sensors.
Using Side Information
If you know the door is only on one side, you can do one-direction exponential search and then binary search inside the first interval that brackets the target condition. That reduces travel and probes.
If you only have probabilistic side hints, bias the order but preserve fallback coverage. Completeness should not depend on uncertain assumptions.
Simulation for Validation
Before deployment, simulate random door positions and compare strategies.
Simulation reveals constant factors and helps choose between theoretically correct variants.
Common Pitfalls
- Searching one direction only on a two-sided infinite domain.
- Expanding radius linearly instead of exponentially.
- Ignoring movement cost when implementing in physical systems.
- Forgetting to probe start position before expansion.
- Measuring only average case and ignoring worst-case guarantees.
Summary
- Infinite two-sided search needs guaranteed coverage strategy.
- Exponential alternating expansion gives completeness with bounded overhead.
- Doubling radius is the key to efficient distant-target discovery.
- Adapt algorithm for movement cost when probes are not free.
- Validate behavior with simulation before production use.
Related reading
- Dots and boxes solving algorithm
- Drawing an antialiased circle as described by Xaolin Wu
- Duplicate a LinkedList with a pointer to a random node apart from the next node
- Duplicate substring searching
- Double or float - optimization routines
- Double.TryParse or Convert.ToDouble - which is faster and safer?
- Dynamic addition of queues to a rabbit listener at runtime
- Dynamic Array with O1 removal of any element

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.