00:00

Problem Statement

Input/Output Examples

Visual Explanation

The key is to create every future first and join last. Calling join() right after each supplyAsync() would make the code sequential again.

Approaches

Both approaches run the three calls on a pool of three threads. The first chains futures; the second hands a list of tasks to the executor.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Thread timing changes from run to run, so the programs print checks (true or false) instead of raw milliseconds; both tabs print the same output.

Complexity Analysis

With one thread per call, the wall-clock time is the time of the slowest call instead of the sum of all calls. Memory grows only with the number of futures.

Interview Tips and Recommended Approach

  • Start everything, then join. Joining inside the loop turns parallel code back into sequential code.
  • Pass your own executor. The default common pool is shared and sized for CPU work, not for slow I/O.
  • Know allOf(). For a list of futures of the same type, CompletableFuture.allOf() waits for all of them.
  • Plan for failure. One failed future fails the combined one; add exceptionally() or a timeout.

Recommended Approach: CompletableFuture and thenCombine()

Why: it keeps the results typed, blocks only once and leaves room for timeouts and fallbacks, which real service calls always need.