Uber Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

public static void main(String[] args) {
int[][] edges = new int[][]{
{1, 2, 2, 3},
{1, 4, 4, 2},
{2, 3, 2, 1},
{4, 5, 1, 2},
{5, 3, 2, 1},
{3, 7, 3, 3},
{7, 6, 1, 2},
{3, 6, 5, 3}
};
System.out.println(shortestPath(7, edges, 1, 6));
}

public static int shortestPath(int n, int[][] edges, int src, int dst) {
Map<Integer, List<Node>> map = new HashMap<>();
for (int[] ed : edges) {
int x = ed[0];
int y = ed[1];
int w1 = ed[2];
int w2 = ed[3];

map.putIfAbsent(x, new ArrayList<>());
map.get(x).add(new Node(y, w1, w2));
}

int[][] shortestPath = new int[2][n+1];
Arrays.fill(shortestPath[0], Integer.MAX_VALUE);
Arrays.fill(shortestPath[1], Integer.MAX_VALUE);
int[][] visited = new int[2][n+1];

PriorityQueue<QueueNode> queue = new PriorityQueue<>();
shortestPath[0][src] = 0;
shortestPath[1][src] = 0;

queue.add(new QueueNode(src, 0, 0));
queue.add(new QueueNode(src, 0, 1));
while (!queue.isEmpty()) {
QueueNode queueNode = queue.poll();
if (queueNode.curvedCount == 0 && visited[0][queueNode.node] == 0) {
visited[0][queueNode.node] = 1;
for (Node link : map.getOrDefault(queueNode.node, new ArrayList<>())) {
shortestPath[0][link.node] = Math
.min(shortestPath[0][link.node], shortestPath[0][queueNode.node] + link.w1);
shortestPath[1][link.node] = Math
.min(shortestPath[1][link.node], shortestPath[0][queueNode.node] + link.w2);
queue.add(new QueueNode(link.node, shortestPath[0][link.node], 0));
queue.add(new QueueNode(link.node, shortestPath[1][link.node], 1));
}
} else if (queueNode.curvedCount == 1 && visited[1][queueNode.node] == 0) {
visited[1][queueNode.node] = 1;
for (Node link : map.getOrDefault(queueNode.node, new ArrayList<>())) {
shortestPath[1][link.node] = Math
.min(shortestPath[1][link.node], shortestPath[1][queueNode.node] + link.w1);
queue.add(new QueueNode(link.node, shortestPath[1][link.node], 1));
}
}
}

System.out.println(Arrays.toString(shortestPath[0]));
System.out.println(Arrays.toString(shortestPath[1]));

return Math.min(shortestPath[1][dst], shortestPath[0][dst]);
}


public static class QueueNode implements Comparable<QueueNode> {

int node;
int dis;
int curvedCount;

public QueueNode(int node, int dis, int curvedCount) {
this.node = node;
this.dis = dis;
this.curvedCount = curvedCount;
}

@Override
public int compareTo(QueueNode o) {
if (dis == o.dis) {
return curvedCount - o.curvedCount;
}
return dis - o.dis;
}
}


public static class Node {

int node;
int w1;
int w2;

public Node(int node, int w1, int w2) {
this.node = node;
this.w1 = w1;
this.w2 = w2;
}
}

- Anonymous February 18, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ShortestPathWithCurve {

public static void main(String[] args) {
int[][] edges = new int[][]{
{1, 2, 2, 3},
{1, 4, 4, 2},
{2, 3, 2, 1},
{4, 5, 1, 2},
{5, 3, 2, 1},
{3, 7, 3, 3},
{7, 6, 1, 2},
{3, 6, 5, 3}
};
System.out.println(shortestPath(7, edges, 1, 6));
}

public static int shortestPath(int n, int[][] edges, int src, int dst) {
Map<Integer, List<Node>> map = new HashMap<>();
for (int[] ed : edges) {
int x = ed[0];
int y = ed[1];
int w1 = ed[2];
int w2 = ed[3];

map.putIfAbsent(x, new ArrayList<>());
map.get(x).add(new Node(y, w1, w2));
}

int[][] shortestPath = new int[2][n+1];
Arrays.fill(shortestPath[0], Integer.MAX_VALUE);
Arrays.fill(shortestPath[1], Integer.MAX_VALUE);
int[][] visited = new int[2][n+1];

PriorityQueue<QueueNode> queue = new PriorityQueue<>();
shortestPath[0][src] = 0;
shortestPath[1][src] = 0;

queue.add(new QueueNode(src, 0, 0));
queue.add(new QueueNode(src, 0, 1));
while (!queue.isEmpty()) {
QueueNode queueNode = queue.poll();
if (queueNode.curvedCount == 0 && visited[0][queueNode.node] == 0) {
visited[0][queueNode.node] = 1;
for (Node link : map.getOrDefault(queueNode.node, new ArrayList<>())) {
shortestPath[0][link.node] = Math
.min(shortestPath[0][link.node], shortestPath[0][queueNode.node] + link.w1);
shortestPath[1][link.node] = Math
.min(shortestPath[1][link.node], shortestPath[0][queueNode.node] + link.w2);
queue.add(new QueueNode(link.node, shortestPath[0][link.node], 0));
queue.add(new QueueNode(link.node, shortestPath[1][link.node], 1));
}
} else if (queueNode.curvedCount == 1 && visited[1][queueNode.node] == 0) {
visited[1][queueNode.node] = 1;
for (Node link : map.getOrDefault(queueNode.node, new ArrayList<>())) {
shortestPath[1][link.node] = Math
.min(shortestPath[1][link.node], shortestPath[1][queueNode.node] + link.w1);
queue.add(new QueueNode(link.node, shortestPath[1][link.node], 1));
}
}
}

System.out.println(Arrays.toString(shortestPath[0]));
System.out.println(Arrays.toString(shortestPath[1]));

return Math.min(shortestPath[1][dst], shortestPath[0][dst]);
}


public static class QueueNode implements Comparable<QueueNode> {

int node;
int dis;
int curvedCount;

public QueueNode(int node, int dis, int curvedCount) {
this.node = node;
this.dis = dis;
this.curvedCount = curvedCount;
}

@Override
public int compareTo(QueueNode o) {
if (dis == o.dis) {
return curvedCount - o.curvedCount;
}
return dis - o.dis;
}
}


public static class Node {

int node;
int w1;
int w2;

public Node(int node, int w1, int w2) {
this.node = node;
this.w1 = w1;
this.w2 = w2;
}
}

}

- vishal February 18, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ShortestPathWithCurve {

    public static void main(String[] args) {
        int[][] edges = new int[][]{
            {1, 2, 2, 3},
            {1, 4, 4, 2},
            {2, 3, 2, 1},
            {4, 5, 1, 2},
            {5, 3, 2, 1},
            {3, 7, 3, 3},
            {7, 6, 1, 2},
            {3, 6, 5, 3}
        };
        System.out.println(shortestPath(7, edges, 1, 6));
    }

    public static int shortestPath(int n, int[][] edges, int src, int dst) {
        Map<Integer, List<Node>> map = new HashMap<>();
        for (int[] ed : edges) {
            int x = ed[0];
            int y = ed[1];
            int w1 = ed[2];
            int w2 = ed[3];

            map.putIfAbsent(x, new ArrayList<>());
            map.get(x).add(new Node(y, w1, w2));
        }

        int[][] shortestPath = new int[2][n+1];
        Arrays.fill(shortestPath[0], Integer.MAX_VALUE);
        Arrays.fill(shortestPath[1], Integer.MAX_VALUE);
        int[][] visited = new int[2][n+1];

        PriorityQueue<QueueNode> queue = new PriorityQueue<>();
        shortestPath[0][src] = 0;
        shortestPath[1][src] = 0;

        queue.add(new QueueNode(src, 0, 0));
        queue.add(new QueueNode(src, 0, 1));
        while (!queue.isEmpty()) {
            QueueNode queueNode = queue.poll();
            if (queueNode.curvedCount == 0 && visited[0][queueNode.node] == 0) {
                visited[0][queueNode.node] = 1;
                for (Node link : map.getOrDefault(queueNode.node, new ArrayList<>())) {
                    shortestPath[0][link.node] = Math
                        .min(shortestPath[0][link.node], shortestPath[0][queueNode.node] + link.w1);
                    shortestPath[1][link.node] = Math
                        .min(shortestPath[1][link.node], shortestPath[0][queueNode.node] + link.w2);
                    queue.add(new QueueNode(link.node, shortestPath[0][link.node], 0));
                    queue.add(new QueueNode(link.node, shortestPath[1][link.node], 1));
                }
            } else if (queueNode.curvedCount == 1 && visited[1][queueNode.node] == 0) {
                visited[1][queueNode.node] = 1;
                for (Node link : map.getOrDefault(queueNode.node, new ArrayList<>())) {
                    shortestPath[1][link.node] = Math
                        .min(shortestPath[1][link.node], shortestPath[1][queueNode.node] + link.w1);
                    queue.add(new QueueNode(link.node, shortestPath[1][link.node], 1));
                }
            }
        }

        System.out.println(Arrays.toString(shortestPath[0]));
        System.out.println(Arrays.toString(shortestPath[1]));

        return Math.min(shortestPath[1][dst], shortestPath[0][dst]);
    }


    public static class QueueNode implements Comparable<QueueNode> {

        int node;
        int dis;
        int curvedCount;

        public QueueNode(int node, int dis, int curvedCount) {
            this.node = node;
            this.dis = dis;
            this.curvedCount = curvedCount;
        }

        @Override
        public int compareTo(QueueNode o) {
            if (dis == o.dis) {
                return curvedCount - o.curvedCount;
            }
            return dis - o.dis;
        }
    }


    public static class Node {

        int node;
        int w1;
        int w2;

        public Node(int node, int w1, int w2) {
            this.node = node;
            this.w1 = w1;
            this.w2 = w2;
        }
    }

}

- vishal February 18, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
        int[][] edges = new int[][]{
            {1, 2, 2, 3},
            {1, 4, 4, 2},
            {2, 3, 2, 1},
            {4, 5, 1, 2},
            {5, 3, 2, 1},
            {3, 7, 3, 3},
            {7, 6, 1, 2},
            {3, 6, 5, 3}
        };
        System.out.println(shortestPath(7, edges, 1, 6));
    }

    public static int shortestPath(int n, int[][] edges, int src, int dst) {
        Map<Integer, List<Node>> map = new HashMap<>();
        for (int[] ed : edges) {
            int x = ed[0];
            int y = ed[1];
            int w1 = ed[2];
            int w2 = ed[3];

            map.putIfAbsent(x, new ArrayList<>());
            map.get(x).add(new Node(y, w1, w2));
        }

        int[][] shortestPath = new int[2][n+1];
        Arrays.fill(shortestPath[0], Integer.MAX_VALUE);
        Arrays.fill(shortestPath[1], Integer.MAX_VALUE);
        int[][] visited = new int[2][n+1];

        PriorityQueue<QueueNode> queue = new PriorityQueue<>();
        shortestPath[0][src] = 0;
        shortestPath[1][src] = 0;

        queue.add(new QueueNode(src, 0, 0));
        queue.add(new QueueNode(src, 0, 1));
        while (!queue.isEmpty()) {
            QueueNode queueNode = queue.poll();
            if (queueNode.curvedCount == 0 && visited[0][queueNode.node] == 0) {
                visited[0][queueNode.node] = 1;
                for (Node link : map.getOrDefault(queueNode.node, new ArrayList<>())) {
                    shortestPath[0][link.node] = Math
                        .min(shortestPath[0][link.node], shortestPath[0][queueNode.node] + link.w1);
                    shortestPath[1][link.node] = Math
                        .min(shortestPath[1][link.node], shortestPath[0][queueNode.node] + link.w2);
                    queue.add(new QueueNode(link.node, shortestPath[0][link.node], 0));
                    queue.add(new QueueNode(link.node, shortestPath[1][link.node], 1));
                }
            } else if (queueNode.curvedCount == 1 && visited[1][queueNode.node] == 0) {
                visited[1][queueNode.node] = 1;
                for (Node link : map.getOrDefault(queueNode.node, new ArrayList<>())) {
                    shortestPath[1][link.node] = Math
                        .min(shortestPath[1][link.node], shortestPath[1][queueNode.node] + link.w1);
                    queue.add(new QueueNode(link.node, shortestPath[1][link.node], 1));
                }
            }
        }

        System.out.println(Arrays.toString(shortestPath[0]));
        System.out.println(Arrays.toString(shortestPath[1]));

        return Math.min(shortestPath[1][dst], shortestPath[0][dst]);
    }


    public static class QueueNode implements Comparable<QueueNode> {

        int node;
        int dis;
        int curvedCount;

        public QueueNode(int node, int dis, int curvedCount) {
            this.node = node;
            this.dis = dis;
            this.curvedCount = curvedCount;
        }

        @Override
        public int compareTo(QueueNode o) {
            if (dis == o.dis) {
                return curvedCount - o.curvedCount;
            }
            return dis - o.dis;
        }
    }


    public static class Node {

        int node;
        int w1;
        int w2;

        public Node(int node, int w1, int w2) {
            this.node = node;
            this.w1 = w1;
            this.w2 = w2;
        }
    }

- Anonymous February 18, 2020 | 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