Bloomberg LP Interview Question for Applications Developers


Country: United States
Interview Type: Phone Interview




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

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

using namespace std;

int main()
{
	vector<string> v;
	v.push_back("A");
	v.push_back("C");
	v.push_back("D");
	v.push_back("E");
	v.push_back("C");
	v.push_back("D");
	v.push_back("E");
	v.push_back("F");
	
	map<string,string> m;
	for(int i=0;i<v.size();i +=2)
		m.insert(make_pair(v[i],v[i+1]));
	
	map<string,string>::iterator it = m.begin();
	string nextKey = it->first;
	int size = m.size();
	string route = "";
	while(size--)
	{
		it = m.find(nextKey);
		if(it != m.end())
		{
			nextKey = it->second;
			cout<<it->first<<"->";
		}
		else
			break;
		
		
	}
cout<<it->second;

}

- Preeti April 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a topological sorting problem. Use the vector<String> to build the graph. Then do topological sorting on it. Below is my code. For more information, please visit my website: allenlipeng47.com/PersonalPage/index/view/105/nkey

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

public class TopogicalSorintDFS {

	public static void main(String[] args) {
		Vector<String> airports = new Vector<String>();
		airports.add("JFK");
		airports.add("LAX");
		airports.add("SNA");
		airports.add("RKJ");
		airports.add("LAX");
		airports.add("SNA");
		Vector<String> result = findPath(airports);
		System.out.println(result.toString());
	}

	public static Vector<String> findPath(Vector<String> airports) {
		Map<String, AirNode> nodes = new HashMap<String, AirNode>();
		// construct graph
		for (int i = 0; i < airports.size(); i += 2) {
			String from = airports.get(i);
			String to = airports.get(i + 1);
			if (!nodes.containsKey(from)) {
				nodes.put(from, new AirNode(from));
			}
			if (!nodes.containsKey(to)) {
				nodes.put(to, new AirNode(to));
			}
			nodes.get(from).outgoing.add(nodes.get(to));
		}
		Vector<String> result = new Vector<String>();
		HashSet<String> visited = new HashSet<String>();
		//start the dfs on graph
		for (AirNode node : nodes.values()) {
			dfs(node, visited, result);
		}
		return result;
	}

	public static void dfs(AirNode node, HashSet<String> visited,
			Vector<String> result) {
		if (visited.contains(node.airport)) {
			return;
		}
		visited.add(node.airport);
		for (AirNode curr : node.outgoing) {
			dfs(curr, visited, result);
		}
		result.insertElementAt(node.airport, 0);
	}
}

class AirNode {
	String airport;
	Set<AirNode> outgoing;

	public AirNode(String airport) {
		this.airport = airport;
		outgoing = new HashSet<AirNode>();
	}

	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		if (obj instanceof AirNode) {
			AirNode node = (AirNode) obj;
			return this.airport.equals(node.airport);
		}
		return false;
	}

	public int hashCode() {
		return this.airport.hashCode();
	}
}

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

This is not DFS problemm. Shortest route might be different. Bellman Ford has to be used.

- max March 12, 2015 | 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