Sidecar Pattern
Sidecar pattern is a useful coding strategy for a coding challenge.
Analyzing coding challenges, I distilled a useful coding pattern to tackle certain types of problems. I call this the sidecar pattern.
If the problem requires you to iterate over an array, you might use the sidecar pattern. It is a strategy that you define another array called sidecar, and run through the original array writing necessary data to the sidecar. Thanks to this additional memory, you might get benefits such as:
- A fewer number of "counting" activity
- Explainable code
Because the space complexity of an array problem is already O(n)
, you might employ a sidecar without sacrificing a lot in terms of space.
Here is a sample problem and my answer using the sidecar pattern. At first, I solved this problem without a sidecar and ended up with a O(n^2)
solution. However, thanks to the sidecar pattern, my current answer is O(n)
.