Uber | Phone Screen | Implement a quad tree

7628
Jan 06, 2018
Aug 02, 2019

I got an interesting question from Uber, it's a phone interview.

Implement a quadtree with its properties:

  Each node has four children (non-value) or one value.

For example:

      Root
 A   B   C            D
 1   2   3       E  F  G  H
                 4  5  6  7

  ** Implement a quadtree and then write an algorithm to convert a 2D-matrix into a quadtree format.

For example:

  [[1,2,3,4],       1 2 | 3 4                  Root   
   [5,6,7,8], >>>   5 6 | 7 8   >>>    TL     TR      BL    BR
   [9,0,5,5],       ---------         1256   3478    9091    5
   [9,1,5,5]]       9 0 | 5 5     
                    9 1 | 5 5

Note: TL means top-left, TR means top-right, BL means bottom-left, BR means bottom-right.

As shown in the description above, for some quarters like bottom right BR, it has all 5s, so the quadtree only stores one value (number 5) instead of storing 4 numbers (5 5 5 5).

Thanks.

Comments (6)