Pergunta de entrevista da empresa Google

Write a Java program that takes a 2D bitmap (represented as a 1D array of integers), and reverses it about its vertical axis.

Respostas da entrevista

Sigiloso

29 de jan. de 2012

I asked the interviewer whether I'm also provided with a width, so that I know how to interpret the data in the 1D bitmap, and he said yes. From a high level, you need a doubly nested for loop: the outer one iterates over the rows, and the inner one iterates over the elements, reversing them as you go along.

Sigiloso

16 de abr. de 2013

In Python: array1d = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] #------------------------------------------------- def flipV(arr, width): rows = len(arr)//width for i in range(rows): # [0..rows) start = i * width # index of first element in row end = start + width - 1 # index of last element in row while start < end: arr[start],arr[end] = arr[end],arr[start] start += 1 end -= 1 #------------------------------------------------- def print2D(arr, width): rows = len(arr)//width for i in range(rows): # [0..rows) rowStart = i * width # start of row(i) for j in range(width): # [0..width) print(arr[rowStart + j], " ", end='') print() print(array1d) # original print2D(array1d, 4) flipV(array1d, 4) print(array1d) # flipped print2D(array1d, 4)

Sigiloso

16 de abr. de 2013

Result: >>> Input [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Output [3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12] 3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12

Sigiloso

24 de fev. de 2013

function flip(array) { if(array.length === 0) { return []; } var done = false; var pos = 0; var result = []; while(!done) { done = true; result[pos] = 0; for(var i = 0; i >> 1; if(array[i] > 0) done = false; } pos += 1; } return result; }