Amazon.com

www.amazon.com

Interview Question

Software Engineer Intern Interview Seattle, WA

In-place array reversal, level-order binary tree traversal

  , reading two files and outputting strings common to both.
Answer

Interview Answer

1 Answer

0

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

Juan on Feb 4, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.