Google Interview Question for Backend Developers


Country: United States




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

{{
public class IndexofLastDuplicate {

public static void main(String[] args) {
int arr[] = { 1, 2, 5, 6, 6, 7, 9, 9, 9, 9, 9, 9 };
int lastIndex = findLastDuplicateIndex(arr);
System.out.println("Last duplicate" + lastIndex);
}

public static int findLastDuplicateIndex(int[] arr) {
int lastIndex = -1;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] == arr[i + 1]) {
lastIndex = i + 1;
}
}
return lastIndex;
}

}
}}

- gentleman January 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

static int lastIndex(int[] arr)  {
        if (arr == null || arr.length <= 1) return -1;
        int len = arr.length;
        for (int i = len-1; i >0; i--) {
            if (arr[i] == arr[i-1]) return i;
        }
        return -1;
    }

- Aim_Google January 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int findLastIndexOfDuplicateNumber(int[] arr) {
		int len = arr.length - 1;
		for (int i = len; i >= 0; i--) {
			int j = i - 1;
			if (j >= 0 && arr[i] == arr[j]) {
				return i;
			} else {
				continue;
			}

		}
		return -1;

	}

- newbie January 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int lastIndexDuplicateNumber(int[] input) {
        if (input == null) {
            return -1;
        }
        int prev = input[0];
        int lastIndex = -1;
        for (int i = 1; i < input.length; i++) {
            int num = input[i];
            if (prev == num) {
                //duplicate number
                lastIndex = i;
            }
            prev = num;
        }
        return lastIndex;
    }

- Johnb January 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

void DupLastIndex (int *arr, int n) {
  int i;
  
  if (arr == NULL || n <= 0) {
    return;
  }
  
  for (i = n - 1; i > 0; i--) {
    if (arr[i] == arr[i - 1]) {
      printf("Last index: %d\nLast duplicate item: %d\n", i, arr[i]);
      return;
    }
  }
}

int main() {
  int arr[] = {1, 2, 5, 6, 6, 7, 9};
  
  DupLastIndex (arr, sizeof(arr)/sizeof(int));
  
  return 0;
}

- Nitin January 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- Walk the array from right to left.
- Compare i-th item with (i - 1)th item.
- If there's a match, print i. You are done.

#include <stdio.h>

void DupLastIndex (int *arr, int n) {
  int i;
  
  if (arr == NULL || n <= 0) {
    return;
  }
  
  for (i = n - 1; i > 0; i--) {
    if (arr[i] == arr[i - 1]) {
      printf("Last index: %d\nLast duplicate item: %d\n", i, arr[i]);
      return;
    }
  }
}

int main() {
  int arr[] = {1, 2, 5, 6, 6, 7, 9};
  
  DupLastIndex (arr, sizeof(arr)/sizeof(int));
  
  return 0;
}

- Nitin January 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- Walk the array from right to left.
- Compare i-th item with (i - 1)th item.
- If there's a match, print i. You are done.

#include <stdio.h>

void DupLastIndex (int *arr, int n) {
  int i;
  
  if (arr == NULL || n <= 0) {
    return;
  }
  
  for (i = n - 1; i > 0; i--) {
    if (arr[i] == arr[i - 1]) {
      printf("Last index: %d\nLast duplicate item: %d\n", i, arr[i]);
      return;
    }
  }
}

int main() {
  int arr[] = {1, 2, 5, 6, 6, 7, 9};
  
  DupLastIndex (arr, sizeof(arr)/sizeof(int));
  
  return 0;
}

- Nitin January 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- Walk the array from right to left.
- Compare i-th item with (i - 1)th item.
- If there's a match, print i. You are done.

#include <stdio.h>

void DupLastIndex (int *arr, int n) {
  int i;
  
  if (arr == NULL || n <= 0) {
    return;
  }
  
  for (i = n - 1; i > 0; i--) {
    if (arr[i] == arr[i - 1]) {
      printf("Last index: %d\nLast duplicate item: %d\n", i, arr[i]);
      return;
    }
  }
}

int main() {
  int arr[] = {1, 2, 5, 6, 6, 7, 9};
  
  DupLastIndex (arr, sizeof(arr)/sizeof(int));
  
  return 0;
}

- ranechabria January 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(nlogn) using binary search

#include <iostream>
using namespace std;

int FindIndex(int* array, int mid)
{
	if(array[mid-1] == array[mid] && array[mid] == array[mid+1])
	{
		return mid+1;
	}
	else if(array[mid-1] == array[mid])
	{
		return mid;
	}
	else if(array[mid] == array[mid+1])
	{
		return mid+1;
	}
	else
	{
		return -1;
	}
}

int FindLastIndexOfDuplicate2(int* array, int start, int end)
{
	if(start == end)
	{
		return -1;
	}
	if(start+1 == end)
	{
		return (array[start] == array[end] ? end : -1);
	}
	else if(start+2 == end)
	{
		return FindIndex(array, start+1);
	}
	else if(start+3 == end)
	{
		int x = FindIndex(array, start+2);
		return x == -1 ? FindIndex(array, start+1) : x;
	}
	int mid = (end-start)/2 + start;
	if(array[mid-1] == array[mid] && array[mid] == array[mid+1])
	{
		return FindLastIndexOfDuplicate2(array, mid, end);
	}
	else if(array[mid-1] == array[mid])
	{
		return FindLastIndexOfDuplicate2(array, mid-1, end);
	}
	else if(array[mid] == array[mid+1])
	{
		return FindLastIndexOfDuplicate2(array, mid, end);
	}
	else
	{
		return FindLastIndexOfDuplicate2(array, mid+1, end);
	}
}

int FindLastIndexOfDuplicate(int array[], int length)
{
	return FindLastIndexOfDuplicate2(array, 0, length-1);
}
int main() {
	int n;
	cin>>n;
	int A[n];
	for(int i=0; i<n; i++)
	{
		cin>>A[i];
	}
	cout<<FindLastIndexOfDuplicate(A, n);
	return 0;
}

- Nishagrawa March 22, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static Integer getLastIndexOfLastDuplicateNumber(Integer[] arr) {
        ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(arr));

        for (int index = 0; index < numbers.size(); index++) {
            int lastDuplicateElementIndex = numbers.lastIndexOf(numbers.get(index));
            if (lastDuplicateElementIndex > -1 && lastDuplicateElementIndex != index)
                return lastDuplicateElementIndex;
        }
        return -1;
    }

- vvkpd March 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can achieve this with Time Complexity O(n) and Auxiliary Space O(1)

def findLastIndex(arr: [int]) -> int:
	last_index = -1
	for i in range(len(arr)-1):
		 if arr[i] == arr[i + 1]:
			last_index = arr[i + 1]
	return last_index

If the input is not sorted, we can still achieve this with Time Complexity O(n) and Auxiliary Space O(n).

def findLastNotOrdered(arr: [int]) -> int:
	elem_map = {}
	max_index = -1
	for i in range(len(arr)):
		if arr[i] in elem_map:
			elem_map[arr[i]].append(i)
			if len(elem_map[arr[i]]) > 1:
				max_index = i
		else:
			elem_map[arr[i]] = [i]
	return max_index

- nicolarusso March 12, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Loop through array, XOR neighbors, whenever the result is 0, store the number in that index as the last duplicate number.

Space - O(n) + O(1)
Time - O(n)

- Newbie January 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public static int findLastIndexOfDuplicateNumber(int[] arr) {
int len = arr.length - 1;
for (int i = len; i >= 0; i--) {
int j = i - 1;
if (j >= 0 && arr[i] == arr[j]) {
return i;
} else {
continue;
}

}
return -1;

}

- Anonymous January 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public static int findLastIndexOfDuplicateNumber(int[] arr) {
int len = arr.length - 1;
for (int i = len; i >= 0; i--) {
int j = i - 1;
if (j >= 0 && arr[i] == arr[j]) {
return i;
} else {
continue;
}

}
return -1;

}

- Anonymous January 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public static int findLastIndexOfDuplicateNumber(int[] arr) {
		int len = arr.length - 1;
		for (int i = len; i >= 0; i--) {
			int j = i - 1;
			if (j >= 0 && arr[i] == arr[j]) {
				return i;
			} else {
				continue;
			}

		}
		return -1;

	}

- Anonymous January 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Anyone can right O(n), its not O(n) question and


use Binary Search on it for O(log n )

- hprem991 January 12, 2018 | 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