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

- 11.9.95.shubham 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.
-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 1 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