I was asked the following question in a Facebook coding interview.
There is a 2D grid, a cell with value 1 indicates it is occupied and value 0 indicates that it is empty. We are given an object, described as (x, y, width, height) where x, y are the top-left corner of the object, width and height are its dimensions. The task is to implement a function that places a given object onto the grid, if it is impossible to place it at (x, y), please find the nearest location to (x, y) and place it there. Objects should not overlap after the placement.
I can only come up with a brute force solution with O(nmwidth*height). Does anyone know a better solution?