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][0];
            int end = meetings[i][1];

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

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

Would you please explain your answer.

- Mukesh Kumar Behera July 21, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

which algorithm is this can anyone tell

- Anonymous May 24, 2021 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anon July 31, 2021 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Mehul February 25, 2022 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Mehul February 25, 2022 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can anyone tell which algoritm is this?

- bhargavi May 24, 2021 | 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