FastPrepGroup Bounding Boxes by Indentation

Group Bounding Boxes by Indentation

Reducto logoReducto● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

A document contains bounding boxes in reading order. You are given left[i], the left position of box i. Group boxes with similar indentation, number the groups from left to right starting at 1, and return one group number per box in the original input order. Other box properties, including page, do not affect the result.

For this exercise, assume positions have at most three decimal places and use the following fixed-anchor grouping rule:

  1. Take the smallest position not yet grouped as the next group's anchor.
  2. Put every remaining position whose difference from that anchor is at most 0.01 into the group. The threshold is inclusive.
  3. Repeat with the remaining positions, advancing the group number each time.

Compare against the fixed anchor, not against the previous position: a chain of nearby boxes cannot expand a group beyond 0.01 from its anchor. For example, positions 0, 0.009, and 0.018 form two groups.

Return an empty array for no boxes. Preserve the input array. The first input box need not belong to group 1, and later boxes may return to any earlier group.

Function

groupIndentation(left: double[]) → int[]

Examples

Example 1

left = [0.30,0.10,0.305,0.105]return = [2,1,2,1]

The first anchor is 0.10, grouping 0.10 and 0.105. The next anchor is 0.30, grouping 0.30 and 0.305. Restoring the original order gives [2,1,2,1].

Example 2

left = [0.018,0,0.009]return = [2,1,1]

The fixed anchor 0 includes 0.009 but excludes 0.018. The last value does not move the anchor or join the two groups transitively.

Example 3

left = []return = []

There are no boxes to label.

Constraints

  • 0 <= left.length <= 5000.
  • Every position is finite, lies in [0, 1000000], and has at most three decimal places.
  • The inclusive group tolerance is exactly 0.01; fixed anchors and one-based labels follow the stated practice rule.
  • Do not mutate left.

More Reducto problems

See Reducto hiring insights
public int[] groupIndentation(double[] left) {
    // Write your code here.
}
left[0.30,0.10,0.305,0.105]
expected[2,1,2,1]
Checking account…