## 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<>());
}

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;

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<>())) {
}
} else if (queueNode.curvedCount == 1 && visited[1][queueNode.node] == 0) {
visited[1][queueNode.node] = 1;
for (Node link : map.getOrDefault(queueNode.node, new ArrayList<>())) {
}
}
}

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;
}
}

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<>());
}

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;

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<>())) {
}
} else if (queueNode.curvedCount == 1 && visited[1][queueNode.node] == 0) {
visited[1][queueNode.node] = 1;
for (Node link : map.getOrDefault(queueNode.node, new ArrayList<>())) {
}
}
}

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;
}
}

}

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<>());
}

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;

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<>())) {
}
} else if (queueNode.curvedCount == 1 && visited[1][queueNode.node] == 0) {
visited[1][queueNode.node] = 1;
for (Node link : map.getOrDefault(queueNode.node, new ArrayList<>())) {
}
}
}

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;
}
}

}``````

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<>());
}

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;

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<>())) {
}
} else if (queueNode.curvedCount == 1 && visited[1][queueNode.node] == 0) {
visited[1][queueNode.node] = 1;
for (Node link : map.getOrDefault(queueNode.node, new ArrayList<>())) {
}
}
}

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;
}
}``````

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.

### 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.