AlgoMaster Logo
AlgoMasterCompose a Partial BFF Responsemedium

Compose a Partial BFF Response

medium

A Backend for Frontend often calls several services in parallel. Some fields are required for the screen to be valid, while optional fields may use a fallback when their service fails or misses the deadline.

Design a BffResponseComposer class with compose(fields, required, latenciesMs, outcomes, deadlineMs).

The aligned arrays describe parallel calls:

  • required[i] is 1 when field i is required and 0 when it is optional.
  • outcomes[i] is "success" or "failure".
  • latenciesMs[i] is when that result would arrive.

Classify a call as:

  • ok when it succeeds at or before the deadline.
  • unavailable when it fails at or before the deadline.
  • timeout when its latency is greater than the deadline, regardless of its eventual outcome.

Its settlement time is its latency for ok or unavailable, and deadlineMs for a timeout.

If required data is unavailable, return one string: FAILED:field:reason@time. Select the unavailable required field with the earliest settlement time, keeping input order on a tie. The BFF fails as soon as that result is known.

Otherwise return READY:time, followed by field:ok, field:unavailable, or field:timeout for every field in input order. Because calls run in parallel, ready time is the maximum settlement time, not their sum. An empty fan-out returns ["READY:0"].

Example 1:
Example 2:

Constraints

  • 0 <= fields.length == required.length == latenciesMs.length == outcomes.length <= 10^5
  • Field names are unique and contain 1 to 40 printable non-space characters.
  • required[i] is 0 or 1.
  • outcomes[i] is "success" or "failure".
  • 0 <= latenciesMs[i] <= 10^9, 1 <= deadlineMs <= 10^9
  • At most 100 calls are made to compose.
Hints

Loading...
CallReturns
new BffResponseComposer()null
compose(["product","reviews","inventory"], [1,0,1], [40,120,60], ["success","success","success"], 100)["READY:100","product:ok","reviews:timeout","inventory:ok"]

Product and inventory are required and arrive in time. Reviews are optional and exceed the deadline, so the BFF waits until 100 ms and returns a partial response.

Run checks these cases. Submit also runs a larger hidden set.