## 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 int maxInterval(int t, int[][] meetings, int k) {
int prevEnd = 0;
int[] arr = new int[meetings.length + 1];
for (int i = 0; i < meetings.length; i++) {
int st = meetings[i];
int end = meetings[i];

arr[i] = st - prevEnd;
prevEnd = end;
}

arr[meetings.length] = t - prevEnd;
int len = meetings.length;

int sum = 0;
for (int i = 0; i < k && i <= len; i++) {
sum = sum + arr[i];
}

int max = sum;
for (int i = k; i <= len; i++) {
sum = sum - arr[i-k] + arr[k];
max = Math.max(max, sum);
}

return max;
}``````

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

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

which algorithm is this can anyone tell

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

They are basically calculating the gaps between the presentations in the arr array. Now you can notice that the longest time period you can get by at most k reschedules is k+1 consecutive gaps.

For example, if you have gap0, event1, gap1, event2, gap2, event3, gap3, event4, gap4. If k=2, in two reschedules you can switch event[i] with either gap[i] or gap[i-1] to get a series of 3 consecutive gaps. Now, we take the maximum size of k consecutive gaps to get the answer

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

There is a small mistake in the last for loop, instead of arr[i - k] and arr[k] it will be arr[i - k - 1] and arr[i] respectively.

Previous code:

``````for (int i = k; i <= len; i++) {
sum = sum - arr[i-k] + arr[k];
max = Math.max(max, sum);
}``````

Correct code:

``````for (int i = k; i <= len; i++) {
sum = sum - arr[i-k-1] + arr[i];
max = Math.max(max, sum);
}``````

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

There is a small mistake in the last for loop, instead of arr[i - k] and arr[k] it will be arr[i - k - 1] and arr[i] respectively.

Previous code:

``````for (int i = k; i <= len; i++) {
sum = sum - arr[i-k] + arr[k];
max = Math.max(max, sum);
}``````

Correct code:

``````for (int i = k; i <= len; i++) {
sum = sum - arr[i-k-1] + arr[i];
max = Math.max(max, sum);
}``````

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

can anyone tell which algoritm is this?

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.