Skip to the code
cf.
+25 XP

#683 Process Pending Lanes

medium

UI schedulers often keep pending work as bits called lanes. React does this: each lane is a bit, pending work is the OR of lanes, and a lower bit means a more urgent lane. React can also group related lanes so they finish together. This problem models that idea in a simplified way.

pending is a mask of lanes waiting to run. groups is a list of masks; lanes in the same group must be processed together. Each step, the scheduler takes the lowest set bit of pending. If that lane is in one or more groups, every pending lane in those groups joins the same batch. The batch is removed from pending and recorded.

Write laneBatches(pending, groups) that returns the list of batch masks in the order they are processed, until pending is 0.

Example 1

Input: pending = 22, groups = []
Output: [2,4,16]

22 is 10110. With no groups, lanes run one by one from the lowest bit: 2, then 4, then 16.

Example 2

Input: pending = 22, groups = [20]
Output: [2,20]

Lanes 4 and 16 form group 20, so when lane 4 comes up, lane 16 joins its batch.

Constraints

  • 0 <= pending <= 2^30
  • 0 <= groups.length <= 10
  • Each group mask is between 1 and 2^30.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).