Amazon Interview Question
InternsCountry: United States
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]...
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);
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.
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 + " ");
}
}
}
}
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--;
}
}
}
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
its pretty simple i guess: you just need to subtract each element by 1. is. A[i][j][k]..=1-A[i][j][k]..;
- Gitesh February 06, 2013it will simply flip all 0's to 1 and vice versa.It runs in O(i*j*k..) time complexity