Flatten Thunk

Hard
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start•← → to step•? all shortcuts

Solution Guide: Flatten Thunk

Approach

Repeatedly invoke the value while it is a function, replacing it with the return value each time. Continue until the result is no longer a function, then return the final non-function value. Also implement a recursive variant that achieves the same result through self-calls.

Complexity Analysis

Time
O(d) where d is the nesting depth
Space
O(1) iterative, O(d) recursive

Pattern

Thunk Resolution

Why It Works

A thunk is a nullary function wrapping a deferred value, so repeatedly calling it peels away each layer of indirection until the underlying value is reached.

Updated Mar 2026