Oracle Interview Question

Without using length and size function how could you get the 2nd last variable in an array of integers?

Interview Answers

Anonymous

Jul 16, 2016

int[] arr = new int[]{1,2,4,1,4,6,2}; int count = 0; for (int i : arr) { count++; } System.out.println(arr[count-2]);

6

Anonymous

Jun 20, 2016

Use two pointers and handle ArrayOutOfBoundException

7

Anonymous

Jul 18, 2016

Put the elements in a stack. Pop twice. The 2nd element will be the last but 2 in the array. These questions are only useful to make you think better. They don't make sense in real life programming. If you have to encounter such situations however, either that environment is too constrained or it is not a company you want to work for.

1