Airbnb Interview Question

Country: United States

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

Looking for interview experience sharing and coaching?

Visit for private lessons by FB, Google and Uber engineers

Our ONE TO ONE class offers

SYSTEM DESIGN Courses (highly recommended for candidates for FLAG & U)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn and other top tier companies after weeks of training.

Feel free to email us with any questions. Thanks!

Any routing algorithm will solve this problem, such as BFS Dijrsktra's etc.
It's still a challenging problem under the time limit in an interview since it involves first parsing the input string into a graph structure and then run the routing algorithm.

public int minCost(String flights, String from, String to, int k) {
        if(from == to) return 0;
        if(flights.isEmpty()) return -1;

        int[][] edges = parseString(flights,from, to);
        Map<Integer, Integer> minCostSet = new HashMap<>();
        minCostSet.put(0, 0);

        Queue<Integer> prev = new ArrayDeque<>(); //previous level of nodes

        while(k >= 0 && !prev.isEmpty()) {  //BFS
            Queue<Integer> current = new ArrayDeque<>();
            for(int source: prev) {
                for(int destination = 0; destination < edges.length; destination++) {
                    if(edges[source][destination] > 0) {
                        int minCost = minCostSet.containsKey(destination) ? minCostSet.get(destination) : Integer.MAX_VALUE;
                        int newCost = Math.min(minCost, minCostSet.get(source) + edges[source][destination]);
                        if (minCost > newCost) {
                            minCostSet.put(destination, newCost);
            prev = current;
        return minCostSet.containsKey(edges.length - 1) ? minCostSet.get(edges.length - 1): -1;

    private int[][] parseString(String flights, String start, String end) {
        Map<String, Integer> map = new HashMap<>();
        List<int[]> edges = new ArrayList<>();
        String[] f = flights.split(",");
        map.put(start, 0);
        map.put(end, -1);
        for(int i = 0; i < f.length; i+=2) {
            int idx = f[i].indexOf('-');
            String src = f[i].substring(0, idx);
            String des = f[i].substring(idx + 2);
            if(!map.containsKey(src)) {
                map.put(src, map.size() - 1);
                map.put(des, map.size() - 1);
            if(map.get(src) != -1) {    //exclude flights that departs from the ultimate destination
                edges.add(new int[]{map.get(src), map.get(des), Integer.parseInt(f[i + 1])});
        int n = map.size();
        int[][] graph = new int[n][n];
        for(int[] edge: edges) {
            if(edge[1] == -1) edge[1] = n - 1;
            graph[edge[0]][edge[1]] = edge[2];
        return graph;

- aonecoding July 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

Did the question only ask for the min cost or it asks for the optimal path?

- Rabbit War June 03, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

Did the question only ask for min cost or the optimal path(s)?

- Rabbit War June 03, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
of 1 vote

BFS is the way to go

- koustav.adorable July 25, 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