Amazon Interview Question

In-place array reversal, level-order binary tree traversal, reading two files and outputting strings common to both.

Interview Answer

Anonymous

Feb 4, 2014

In Place reverse array: public char[] inPlaceReverse(String s) { char[] array = s.toCharArray(); if (s != null) { int p1 = 0; int p2 = s.length()-1; while (p2 - p1 > 0) { swap(array,p2,p1); p2--; p1++; } } return array; } private void swap(char[] array, int p1, int p2) { char temp = array[p1]; array[p1] = array[p2]; array[p2] = temp; }