FAANG Interview Question
Anonymous User
844

Question:
Given a list of cakes, where each cake is represented by (x,y) coordinates (topleft corner of cake) and size of cake. These cakes are kept on rectangular table, also cake is rectangular. We have to find a position on y-axis such that if we cut cakes (apear on that y-coord) on table total cakes size on both side will be equal

  1. All the cakes are aligned with x and y axis
  2. x,y,size and output cut position all are double values
  3. There is no overlapping of cakes
    Input: Cakes = [cake(x=1.0,y=0.0,size=2.0),cake(x=4.0,y=1.0,size=2.0)], Output: 1.5

Approach 1:

  1. Binary search, to find the cut position on y-axis
  2. Start = min(start of y-coords of all cakes), End = max(end y-coord of all cakes)
  3. If size(start:mid) == size(mid:end): mid is the position
  4. size(start:mid) > size(mid:end): end = mid
  5. else: start = mid
    But interviewer said this won't be optimal, asked me to optimize

Approach 2:
I suggested prefix sum methodology

  1. Iterate (float increament) from start to end
  2. Check leftSize == rightSize: return position
  3. Add portion to leftSize and subtract from rightSize (height = nextPos - curPos (y-coord))

I don't know answer for this, no idea what will be optimal solution, could someone either solve it or give resources if have seen any similar question
Thanks!

Comments (5)