Apple Coding Interview Question: Merge Bounding Boxes

16 Views
No Comments

Given the following 2D Bounding Box struct, write a function to merge n bounding boxes together, and return a final bounding box with the result.

struct BBox {
    float x;  // x position of the box center.
    float y;  // y position of the box center.
    float w;  // width of the box (x direction).
    float h;  // height of the box (y direction).
};

BBox merge(BBox *boxes, int boxCount)
{}

This problem asks you to merge several 2D bounding boxes, each represented by center coordinates and width/height, into one box that covers them all. The usual solution is to convert every box into its left, right, top, and bottom edges, track the extreme values in a single pass, and then reconstruct the final box from those boundaries. It is a straightforward geometry and linear-scan problem, typically solved in O(n) time.

END
 0