Amazon Interview Question for Interns


Country: United States




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

its pretty simple i guess: you just need to subtract each element by 1. is. A[i][j][k]..=1-A[i][j][k]..;
it will simply flip all 0's to 1 and vice versa.It runs in O(i*j*k..) time complexity

- Gitesh February 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But how to know the dimension of the array? like how to implement the generic case?

We can also just neagate it ex: a[i][j][k]..=!a[i][j][k]...

- xxx February 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if we are coding in c or c++ then all the arrays can be represented by void* type. For eg we can have a function like

void reverse(void* fptr, int n){
int* ptr = (int*)fptr;
int count = 0;
while(count < n){
ptr[count] = 1 - ptr[count];
count = count + 1;
}
}

and we can call this as
int a[i][j][k] = {....}
reverse((int*)a, i*j*k);

- Anonymous February 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

calling can also be
int a[i][j][k] = {....}
reverse(a, i*j*k); //no typecasting required

- Anonymous February 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

I think the question is to reverse the order of indices, for example, if it is a 2-dimensional array then "reverse" means to transpose the matrix. For a multi-dimensional array to "reverse" it means to reoder the indices backwards. This is my understanding.

- Chih.Chiu.19 February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In my understanding, your understanding is wrong.

- Phoong Phack Lee February 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the question is to reverse the multi dimesional array not to flip all the bits inside the array. If it was just flipping all the element for 0 to 1 and 1 to 0, XOR with one for every element would have been good, but I do not think that is what the interviewer wants.

- Matru February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

u can simply xor a another array with same dimension whose all value set to 1. ;)

- go4gold February 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not simply use a bitwise XOR on them? I guess doing any integer calculation is slower than bit operation

- Rohan February 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A solution in java to flip values:

public class MDAFlipValue {
    public static void main(String[] args) {
        Object[] array = (Object[])Array.newInstance(Integer.class, 2, 3, 4);
        
        System.out.println("init");
        explore((Object[])array, new RandomInitializer());
        print(array);
        
        System.out.println("flipping values");
        explore((Object[])array, new ValueFlip());
        print(array);
    }
    
    /* multidim array explore */
    
    public static void explore(Object[] array, ArrayAction callback){
        for(int i=0; i<array.length; i++){
            Object cell = array[i];
            if(cell instanceof Object[]){
                explore((Object[])cell, callback);
            }
            else{
                callback.cellAction(array, i);
            }
        }
    }

    public interface ArrayAction{
        public void cellAction(Object[] parentArray, int index);
    }

    /* array edition */
    
    public static class RandomInitializer implements ArrayAction{
        Random random = new Random();
        public void cellAction(Object[] parentArray, int index) {
            parentArray[index] = random.nextInt(2); // 0 or 1
        }
    }
    
    public static class ValueFlip implements ArrayAction{
        public void cellAction(Object[] parentArray, int index) {
            Object value = parentArray[index];
            if(value instanceof Integer){
                Integer v = (Integer) value;
                if(v == 1)
                    parentArray[index] = 0;
                else if(v == 0)
                    parentArray[index] = 1;
                else
                    throw new IllegalArgumentException("unexpected value");
            }
        }
    }
    
    /* console output */
    
    public static void print(Object[] array){
        StringBuffer sb = new StringBuffer();
        print(array, 0, sb);
        System.out.println(sb.toString());
    }
    
    public static void print(Object[] array, int depth, StringBuffer sb){
        for(int i=0; i<array.length; i++){
            Object cell = array[i];
            if(cell instanceof Object[]){
                print((Object[])cell, depth+1, sb);
                sb.append("\n");
            }
            else{
                for (int j = 0; j < depth; j++) 
                    sb.append(" ");
                sb.append(cell + " ");
            }
        }
    }
}

- martin.pernollet February 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A solution in java to flip array dimensions. I think this was the actual question... little more difficult, but more fun and interesting.
Could not do better than O(i*j*k...)

public class MDAFlipDimensions {
    public static void main(String[] args){
        Object[] array = (Object[])Array.newInstance(Integer.class, 2, 3, 4);
        
        System.out.println("init");
        MDAFlipValue.explore((Object[])array, new MDAFlipValue.RandomInitializer());
        MDAFlipValue.print(array);
        
        MDAFlipDimensions fd = new MDAFlipDimensions();
        Object[] flipped = fd.flip(array);
        MDAFlipValue.print(flipped);
        System.out.println("done");
    }
        
    public Object[] flip(Object[] array){
        int dims = computeDimensions(array, 0); 
        
        int[] sizes = new int[dims];
        computeSizes(array, 0, sizes); 
        
        // init dest
        int[] flippedSizes = clone(sizes);
        reverse(flippedSizes);
        Object[] flippedArray = (Object[])Array.newInstance(Integer.class, flippedSizes);
        
        // copy flipped
        int[] path = new int[dims];
        copyFlipped(array, 0, path, flippedArray); 
        return flippedArray;
    }

    // O(1)
    protected int computeDimensions(Object[] array, int depth) {
        if(array[0] instanceof Object[])
            return computeDimensions((Object[])array[0], depth+1);
        else
            return depth+1;
    }

    // O(1)
    protected void computeSizes(Object[] array, int depth, int[] sizes) {
        sizes[depth] = array.length;
        if(array[0] instanceof Object[])
            computeSizes((Object[])array[0], depth+1, sizes);
    }

    // O(i*j*k*...)  
    protected void copyFlipped(Object[] src, int depth, int[] path, Object[] dest) {
        for(int i=0; i<src.length; i++){
            int[] myPath = clone(path); // O(1)
            myPath[depth] = i;
            
            Object cell = src[i];
            if(cell instanceof Object[]){
                // go recursively in sub array
                copyFlipped((Object[])cell, depth+1, myPath, dest);
            }
            else{
                // copy cell in destination array
                insertFlipped(dest, myPath, (Integer)cell, 0);
            }
        }
    }
    
    // O(1) 
    // index is the position in src array: input[i,j,k] gives index {i,j,k}
    protected void insertFlipped(Object[] newArray, int[] index, int value, int depth) {
        int le = index.length-1;
        int id = index[le-depth];
        if(depth<le){
            Object[] childArray = (Object[])newArray[id];
            insertFlipped(childArray, index, value, depth+1);
        }
        else{
            newArray[id] = value;
        }
    }

    /* utils */
    
    protected int[] clone(int[] array){
        int[] clone = new int[array.length];
        System.arraycopy( array, 0, clone, 0, array.length );
        return clone;
    }
    
    protected void reverse(int[] array) {
        int left = 0;
        int right = array.length - 1;

        while( left < right ) {
            int temp = array[left];
            array[left] = array[right];
            array[right] = temp;
            left++;
            right--;
        }
    }
}

- martin.pernollet February 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

question is easy they just want to know your pointer concepts you must know how many elements are present in the array ....code is
int main()
{
int arr[a][b][c][d][e];//any order does not matter
// and number of element in the array is a*b*c*d*e
invert(arr,a*b*c*d*e);
}
void invert(void *ptr,int count)
{
int* a=(int*)ptr;
for(int i=0;i<count;i++)
a[i]^=1;
}
this code done well because array elements are sorted in row order/column order :)
Hop u understand this

- goelrinku90 April 03, 2013 | 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