Bloomberg LP Interview Question for Applications Developers


Country: United States
Interview Type: Phone Interview




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

How about O(n) complexity and O(n) memory:

public static ArrayList<String> getPath(String[] arr){
	HashSet<String> fromSet = new HashSet<String>(arr.length / 2);
	HashMap<String, String> toMap = new HashMap<String, String>(arr.length / 2);
	from(int i = 0; i < arr.length -1; i += 2){
		fromSet.add(arr[i+1]);
		toMap.put(arr[i], arr[i+1]);
	}

	String start = null;
	for(String str : toMap.getKeys()){
		if(!fromSet.contains(str)){
			start = str;
			break;
		}
	}

	ArrayList<String> results = new ArrayList<String>(arr.length / 2 + 1);
	while(start != null){
		results.add(start);
		start = toMap.get(start);
	}
	return results;
}

- zortlord January 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Works fine

- Anonymous January 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Isn't this nLogn complexity? map.insert takes logn and it's performed in another n loop

- Ian February 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

for hash map, it can be minimum n or maximum n^2

- Ian February 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

contains() conatins looping internally. so nlogn.

- asbear February 17, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

def recurs_dfs(graph,start,path=[]):
    path = path+[start]
    for node in graph[start]:
        if node not in path:
            path = recurs_dfs(graph,node, path)
    return path


graph = {'JFK':['LXA'],'LXA':['SNA'],'SNA':['RKJ'],'RKJ':['JFK']}
print recurs_dfs(graph,'JFK')

}

- pavantrinath March 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public String findPath (String [] airports){
    	HashSet <String> set = new  HashSet<> ();
    	HashMap<String,String> map = new HashMap<> ();
    	for (int i = 0 ; airports.length - 2 >= i  ; i += 2){
    		map.put(airports[i], airports[i+1]);
    		set.add(airports[i+1]) ;
    	}
    	String from = null ;
    	for (String c : airports) {
    		if (!set.contains(c)) 
    			from = c ;
    	}  
    	StringBuilder sb = new StringBuilder ();
    	sb.append(from) ;
    	while (map.containsKey(from)) {    	   
    	   	sb.append("->") ;
    	   	sb.append(map.get(from)) ;
    	   	from = map.get(from) ;
    	}    	
    	return sb.toString() ;

}

- Anonymous January 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

What if there is a airport that has path to two airport? I think ,this solution does not work!!
for example,
let the array is {1,2,3,4,1,3,5,3,2,5,2,4}
so the paths are
1->2
3->4
1->3
5->3
2->5
2->4

So, the solution should be 1->2->5->3->4
But, your algorithm is return 1->3->4

- srcnaks February 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can solved by the following.
- Add the pairs(JFK -> LXA, SNA -> RKJ, LXA -> SNA ) in List to a hashMap as key value pair.
- Maintain a visited set which just holds the city names.
- Then start fetching the elements from the start element in the list from hashMap. While fetching add the key in the visited Set.
- Add the retrieved key value pair in the result list which you are going to return
- Stop when you cannot find value for the given key or the retrieved value is already in the visited set(which will stop a cycle).

- Dhass January 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Count the occurences of the city.
the city that are appearing only once are going to be source and destination.
Pick any one of them and if there is a pplace exists that can be reached from there, then for sure its source,else other one is source.Once you get the source just start traversing.

- Hector January 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class FindPath {
    
    public static void main(String[] args) {
        
        List<String> listOfAirPorts = new ArrayList<String>();
        listOfAirPorts.add("JFK");
        listOfAirPorts.add("LXA");
        listOfAirPorts.add("SNA");
        listOfAirPorts.add("RKJ");
        listOfAirPorts.add("LXA");
        listOfAirPorts.add("SNA");
        
        // check if the even string is present in any odd string. if not present then it's the departure airport
        String departure = null;
        String firstDestination = null;
        int departureId = 0;
        boolean depFoundInDest = false;
        for(int i=0; i < listOfAirPorts.size(); i=i+2) {
            for(int j=1; j < listOfAirPorts.size(); j=j+2) {
                if(listOfAirPorts.get(i).equals(listOfAirPorts.get(j))) {
                    depFoundInDest = true;
                }
            }
            
            if (!depFoundInDest) {
                departure = listOfAirPorts.get(i);
                firstDestination = listOfAirPorts.get(i+1);
                departureId = i;
            } else {
                depFoundInDest = false;
            }
        }

        
        // since they are in pairs and we know the departure,
        // we can start with departure, take it's destination, find the departure element of the destination, 
        // so on and so forth until we cannot find a departure for a destination
        
        String path = loopList(departure, firstDestination, listOfAirPorts);
        
        System.out.println(path);
        
    }
    
    private static String loopList(String departure, String destination, List<String> listOfAirPorts) {
        
        for(int i=0; i < listOfAirPorts.size(); i=i+2) {
            if(listOfAirPorts.get(i).equals(destination)) {
                departure = departure + "-->" + destination;
                return loopList(departure, listOfAirPorts.get(i+1), listOfAirPorts);
            }
        }
        departure = departure + "-->" + destination;
        return departure;
        
    }

}

- Sri January 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is O(n^2) in complexity.

- zortlord January 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume all the airports to be a graph and based on the vector connect all the nodes of the graph.. Finally do a traversal..

- Prabakaran January 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Imagine that each string is a node in a graph G. Then each pair represents an edge E between the nodes. (i) Buld this graph G and (ii) then explore it by a DFS or BFS from source node. A DFS example is below:

// G - undirected graph, v - vertex (location id)
	int[] pathTo;
	boolean[] isVisited[];

	public Iterable findPath(Graph G, int source, int destination) {
		int N = G.V(); 		// # of vertices (locations)
		for (int k=0; k<N; k++) { 	// init arrays
			pathTo[k] = -1;
			isVisited[k] = false;
		}
		// DFS path search
		dfs(G, source);
		// Path does not exist
		if (pathTo[destionation] == -1) 	return null;
		// Backtrack while pushing to the stack
		Iterable out<Integer> = Stack<Integer>();
		int aux = pathTo[destination];
		while(aux != -1) {
			out.push(aux);
			aux = pathTo[aux];
		}
		return out;
	}
	
	public void dfs (Graph G, int v) { 
		if (isVisited[v])
			return;
		isVisited[v] = true;
		// recursively visi all adjacent nodes
		for (int w : G.adjacent(v))
			if (!isVisited[w]) {
				dfs(G, w);
				pathTo[w] = v;
			}
	}

- autoboli January 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I was trying this approach too until I reread the problem and it said that you don't know the source and destinations.

- zortlord January 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What I suggest is:
Take a hashmap <src, (pointer, dest)> = city and pointer to node in doubly linked list
Take a doubly linked list with string value of city

Now for each (source, dest.) pair in vector
	take source city from vector and try to find in hashmap
	if not found
		try to find destination city in hashmap
		if found
			using the pointer value from hashmap prepend this (source, dest) pair to DLL and also update hashmap for source city
		if not found create linked pair of nodes (source, dest) and update hashmap with source city
	if found
		there is some error in input

Finally the head and tail of DLL will be the given source and destination and this DLL can be converted to required vector

- Bharat Kumar Arya January 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I did it with using hashmap and just printing it recursively. Let me know if anything wrong with that. Interesting question btw.

package testing;

import java.util.HashMap;

/**
 *
 * @author Chetan
 */
public class airportProblem {
    public static void main(String[] args){
        String[] arr = {"JFK","LXA","SNA", "RKJ", "LXA", "SNA"};
        HashMap z1 = new HashMap();
        
        for(int i = 0; i < arr.length; i = i+2)
        {
            z1.put(arr[i], arr[i+1]);
        }
        String source = "", destination = "";
        boolean inKeys, inValues;
        for(int i = 0; i < arr.length; i++)
        {
            inKeys = z1.keySet().contains(arr[i]);
            inValues = z1.values().contains(arr[i]);
            if(inKeys && !inValues)
                source = arr[i];
            else if (!inKeys && inValues)
                destination = arr[i];
        }
        System.out.println("source = " + source + " destination = " + destination);
        printMapRecursive(z1, source);
        
    }
    
    public static void printMapRecursive(HashMap z1, String Node){
        System.out.print(Node + " ");
        if(z1.get(Node) == null)
            return;
        else
        {            
            printMapRecursive(z1, z1.get(Node).toString());
        }
    }
}

- Chetan Pawar January 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isnt this hamiltonian path problem? Is this not np hard?

- Perserverance January 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
#include <map>
#include <set>

using namespace std;

vector<string> findPath(vector<string> airports) {
    map<string, string> paths;
    set<string> visited;
    vector<string> output;
    for (int i = 0; i <= airports.size()-2; i+=2) {
        paths[airports.at(i)] = airports.at(i+1);
        visited.insert(airports.at(i+1));
    }   
    string from;
    for (int i = 0; i < airports.size(); i++) {
        if (visited.find(airports.at(i)) == visited.end()) {
            from = airports.at(i);
        }   
    }   
    int first = 1;
    while (paths.find(from) != paths.end()) {
        if (!first) {
            output.push_back("->");
        }   
        else {
            first = 0;
        }   
        output.push_back(from + "->" + paths[from]);
        from = paths[from];
    }   
    return output;
}

- Anono February 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class AirportNode{
		private String name;
		private ArrayList<AirportNode> from = new ArrayList<AirportNode>();
		private ArrayList<AirportNode> to = new ArrayList<AirportNode>();
		private int longestPath = 0;
		public AirportNode(String name){ 
			this.name = name;
		}
		public String Name(){
			return name;
		}
		public int LongestPathDistance(){
			return longestPath;
		}
		public void addFrom(AirportNode n){
			from.add(n);
			n.updateLongestPath(this);
		}
		public void addTo(AirportNode n){
			to.add(n);
			n.addFrom(this);
		}
		public void updateLongestPath(AirportNode n){
			if(n.LongestPathDistance()>=longestPath){
				longestPath = n.LongestPathDistance()+1;
				to.remove(n);
				to.add(0, n);
				for(AirportNode f : from)
					f.updateLongestPath(this);
			}
		}
		
		public AirportNode getLongestPathNode(){
			return to.size() == 0 ? null : to.get(0);
		}
		
	}
	
	public String findPath (String [] airports){
    	HashMap<String,AirportNode> map = new HashMap<String,AirportNode> ();
    	    	for (int i = 0 ; airports.length - 2 >= i  ; i += 2){
    	    		AirportNode d = null;
    		    	if(!map.containsKey(airports[i])){
    			    	d = new AirportNode(airports[i]);
    	    			map.put(airports[i],d);
    	    		}else
    		    		d = map.get(airports[i]);
    	    		AirportNode a = null;
    	    		if(!map.containsKey(airports[i+1])){
    		    		a = new AirportNode(airports[i+1]);
    			    	map.put(airports[i+1],a);
    	    		}else
    	    			a = map.get(airports[i+1]);
    	    		d.addTo(a);
    	    	}
    	    	AirportNode n = null;
        	for(String d:map.keySet()){
    	    		if(n == null || 
    	    	    	    	n.LongestPathDistance()<map.get(d).LongestPathDistance()) 
    	    			n = map.get(d);
    	    	}
    	 
     	   	StringBuilder sb = new StringBuilder ();
      	  	sb.append(n.Name()) ;
     	   	while ((n = n.getLongestPathNode()) != null) {    	   
    	    	   	sb.append("->") ;
    	    	   	sb.append(n.Name()) ;
      	  	}    	
      	  	return sb.toString() ;
	}

- srcnaks February 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Vector v = new Vector();
		v.add("JFK");
		v.add("LXA");
		v.add("SNA");
		v.add("RKJ");
		v.add("LXA");
		v.add("SNA");
		findpath(v);
	}
	
	
	public static void findpath(Vector<String> v)
	{
		HashMap<String,String> map = new HashMap<String,String>();
		ArrayList<String> list = new ArrayList<String>();
		String depart="";
		for(int i = 0; i < v.size(); i++)
		{
			//build map	
			map.put(v.get(i),v.get(++i));
			
		}
		
		//find departure airport
		for(int i = 0; i<v.size(); i=i+2)
		{
			if(!map.containsValue(v.get(i)))
			{
				depart  = v.get(i);
				break;
			}
		}
		
		String airport = depart;
		for(int i = 0; i < v.size()/2 +1; i++)
		{
			if(i<v.size()/2)
				System.out.print(airport+"=>");
			else
				System.out.print(airport);
			airport = map.get(airport);	
		}
	}
	
}

- naomi.lijing@googlemail.com February 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <algorithm>
#include <vector>
#include <iostream>
#include <stack>
#include <sstream> 
#include <queue>
#include <cassert>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>

using namespace std;

class Solution {
public:
    vector<string> findPath(const vector<string>& airports) {
        unordered_map<string, string> next;
        unordered_set<string> hasPre;
        for (int i = 0; i < airports.size(); i += 2) {
            next[ airports[i] ] = airports[i+1];
            hasPre.insert(airports[i+1]);
        }

        string start;
        for (int i = 0; i < airports.size(); i++) {
            if (hasPre.find(airports[i]) == hasPre.end()) {
                start = airports[i];
                break;
            }
        }

        string node = start;
        vector<string> path;
        while (next.find(node) != next.end()) {
            path.push_back(node);
            node = next[node];
        }
        path.push_back(node);
        return path;
    }
};

int main() {
    string data[] = {"JFK", "LXA", "SNA", "RKJ", "LXA", "SNA"};
    vector<string> airports;
    for (int i = 0; i < 6; ++i) {
        airports.push_back(data[i]);
    }

    Solution solution;
    vector<string> path = solution.findPath(airports);
    for (string node : path) {
        cout << node << " ";
    }
    cout << endl;
}

- jianhe25 January 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n) complexity.
Maintain a set --> just to get all unique airports
Maintain a map --> for paths
Maintain a reverse map

The idea is to find the destination first (destination: one doesn't have start node and only end node)... And then backtrack using reverse map..

private static void airportPath() {
        String[] arr = {"jfk", "lxa", "sna", "rkj", "lxa", "sna"};

        Set<String> airports = new HashSet<>();
        Map<String, String> path = new HashMap<>();
        Map<String, String> reversePath = new HashMap<>();
        for (int i=0; i<arr.length-1; i=i+2) {
            path.put( arr[i], arr[i+1] );
            reversePath.put(arr[i+1], arr[i]);
            airports.add(arr[i]); airports.add(arr[i+1]);
        }

        String[] result = new String[airports.size()];
        int counter = result.length-1;

        // find destination : one doesn't have start node and only end node
        String destination = "";
        for (String s : airports) {
            if (!path.containsKey(s)) {
                destination = s;
            }
        }
        result[counter] = destination;

        // start from destination and backtrack
        for (int i=counter-1; i>=0; --i) {
            result[i] = reversePath.get(destination);
            destination = result[i];
        }

        for (String i : result) {
            System.out.println(i);
        }
    }

- shekhar2010us February 06, 2017 | Flag Reply


Add a Comment
Name:

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

Books

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

Videos

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