tungwaiyip.info

home

about me

links

Blog

< February 2010 >
SuMoTuWeThFrSa
  1 2 3 4 5 6
7 8 910111213
14151617181920
21222324252627
28      

past articles »

Click for San Francisco, California Forecast

San Francisco, USA

 

smallworld solution (Facebook programming puzzle)

Facebook has published a number of programming puzzles to challenge software developers. Their problems are not trivial at all. This is my attempt on "Snack" level problem It's A Small World. Even this one take me sometimes to solve.

The idea is that there are n points on a 2-dimensional plane. For every point, we have to find the 3 nearest neighbors. The naive solution simply match all possible pairs, resulting in O(n2) complexity. The problem requires us to find a better solution than O(n2).

My idea is to divide the plane into a regular grid like below. For example, to find the neighbor of point A, we can focus on cell 2 in the center top and pretty much ignore the points in cell 9 in the lower right. This works very well if the points are randomly distributed. But if many points are clustered together like cell 9, it will work less well. We can recursively partition it into smaller region. But the basic plan should be suffice for this puzzle. The subdivision is a very simple O(n) process.

grid

To search for the nearest neighbor, we start from points from the same cell. If this doesn't find the three nearest neighbors we extend into the neighboring cells. The pseudo code is shown below:

  result = None

  for step in 0...n

    cells = find cells "step" away from the source
    min_distance = minimum distance from source to the boundary of the cells

    for all point in cells
        insert distance(source, point) into result

    result = 3 closest points in result

    if there exists 3 points in result those distance < min_distance
        return result

  end for

Of course I'm not the first one to consider these algorithm. Wikipedia has some information on the Nearest neighbor search problem. My solution code is available here. (2010-02-21 Facebook's puzzle server is down. So the solution has not been validated.)

I have decided to put the algorithm into good use. I used it to generate a map of all sizable cities in the world and their nearest neighbors. With 20,000 cities a naive algorithm will need 400 million pairing. Using this algorithm only 10 million pairing are need. It is far from optimal, but good enough to handle Facebook's problem. Also I am curious to use it to find the loneliest city in the world, that is, a city those nearest neighbor is the furthest away. Check the map to find out which city is the loneliest city in the world!

2010.02.21 [] - comments

 

 

blog comments powered by Disqus

past articles »

 

BBC News

 

Raqqa: IS 'capital' falls to US-backed Syrian forces (17 Oct 2017)

 

'Islamic State': Raqqa's loss seals rapid rise and fall (17 Oct 2017)

 

Murdered journalist's son hits out at Malta (17 Oct 2017)

 

Drone shows Rohingyas fleeing (16 Oct 2017)

 

Indonesia massacres: Declassified US files shed new light (17 Oct 2017)

 

Man Booker Prize: George Saunders wins for Lincoln in the Bardo (17 Oct 2017)

 

Reese Witherspoon: I was assaulted at 16 (17 Oct 2017)

 

Trump's latest travel ban order blocked (17 Oct 2017)

 

Catalonia: Protests after Spain detains separatists (17 Oct 2017)

 

Weinstein scandal: Game of Thrones actress 'felt powerless' (17 Oct 2017)

more »

 

SF Gate

 

Bay Area News (7 Jan 2012)

 

City Insider (11 Feb 2012)

 

Crime Scene (13 Feb 2012)

 

C.W Newius Column (10 Jan 2012)

 

C.W. Nevius Blog (11 Feb 2012)

 

Education News (10 Jan 2012)

 

KALW (11 Feb 2012)

 

Matier and Ross Blog (11 Feb 2012)

 

Larry Page’s urban innovation unit picks Toronto for first digital neighborhood (17 Oct 2017)

 

Google designs its first chip in-house for Pixel 2 camera (17 Oct 2017)

 

Ex-workers sue Tesla over racist drawings, slurs at Fremont factory (17 Oct 2017)

 

Facebook gets honest, Venmo hits stores, and Oakland students go on Sprint (17 Oct 2017)

 

Aimmune in Brisbane teams with Regeneron to find peanut allergy cure (17 Oct 2017)

 

Ship traffic, Oct. 18 (17 Oct 2017)

more »

 


Site feed Updated: 2017-Oct-17 15:00