Facebook Interview Question for Software Engineers

Country: United States

Comment hidden because of low score. Click to expand.
of 6 vote

This can be solved by maintaining a max heap of size 5. For any given point p2 from the list of n points, we determine and do following.
1) if the heap size is < 5 , add (distance of p2 from p1, point p2) pair to heap.
2) if heap size >= 5, we check if the distance of p2 from target point p1 is < distance of root node.
2a) if yes, we remove root node and add (distance of p2 from p1, point p2) pair to heap
2b) if no, ignore p2

In the end we will be left with 5 closest point to point p1.

- Anonymous April 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
of 3 vote

Divide your map into a recursive grid.
Each cell contains either: a list of locations; or, if the number of locations in the cell exceeds some number N, it is divided into 4 more cells.
Search the cell the point lies in. Then, for each of 8 neighboring cells, if it is possible for a location to be closer than those already found, search that cell too.

- tjcbs2 March 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
of 2 vote

Here is the gist:
1) Forget longitude and latitude, its just a weighted graph
2) Run any shortest path algo like Dijkstra algorithm to find shortest path of all node from the node in question
3) return the top 5 least paths

public class Solution {
  public List<Node> getClosestNode(Node n, int closest) {
    if(n == null || n.children() == null || n.children().size == 0 || closest <= 0) {
      return new ArrayList<Node>();

    Set<Node> settledNodes = new HashSet<Node>();
    Map<Node, Node> predecessors = new HashMap<>();
    PriorityQueue unsettledNodes = new PriorityQueue(new Comparator<Node>(){
      public int compare(Node n1, Node n2) {
        return n1.getDistance() - n2.getDistance();
    Map<Integer, List<Node>> distance = new TreeMap<>();
    distance.put(0, new ArrayList<>());

    while(!unsettledNodes.isEmpty()) {
      Node source = unsettledNodes.poll();
      List<Node> neighbours = source.neighbours();
      for(Node neighbour : neighbours) {
        if(settledNodes.contains(neighbour )) {
        int newDistance = this.getDistance(source) + n.getDistance();
        if(this.getDistance(distance, neighbour ) > newDistance) {
          if(!distance.hasKey(newDistance)) {
            distance.put(newDistance, new ArrayList());
          distance.get(newDistance).add(neighbour );
          unsettledNodes.add(neighbour );
          predecessors.put(source, neighbour );

    return this.getClosestNode(distance, closest);

  public List<Location> getClosestLocations(Map<Integer, List<Node>> map, int closest) {
    List<Location> locations = new ArrayList<Location>();
    for(EntrySet<Integer, List<Node>> entry : distance) {
      for(Node n : entry.value()) {
        if(locations.size() == closest) {
    return locations;

  public int getDistance(Map<Node, Integer> distance, Node to) {
    return distance.hasKey(to) ? distance.get(to) : Integer.MAX_VALUE;

public class Node {
  private List<Node> neighbours.....
  private Location location;
  private int distance;

- nk March 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

DS: Graph of locations.
Identify the location (node) with the given coordinates.
Return 5 closest nodes.

- Orion arm, Laniakea April 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

Drop recursively decimal places (precision) from longitude and attitude see if there are locations that match that range. With this algorithm, we are only looking at the closest locations first, recursively expand out from the given location.

- LT-testEngineer August 17, 2017 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More