Google Interview Question
1,069 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Developer at Google:
How would you reverse the image on an n by n matrix where each pixel is represented by a bit?
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (5)
{
for (int i=0; i < size; i++) {
for (int j=0; j < size; j++) {
image[i][j] = !image[i][j];
}
}
}
Helpful Answer?
Yes |
No
Inappropriate?
Assumptions: n is even; flip along the vertical axis
ReverseImage (image, length, width)
{
center = width/2
for (i=0 to length) {
for ( j=1 to center) {
if image[i][center-j] != image[i][center-1+j]{
image[i][center-j] = !image[i][center-j]
image[i][center-1+j] = !image[i][center-1+j]
}
}
}
}
Technically length and width are both n but I thought the code is easier to read if we give them separate, meaningful names.
Instead of the if statement, you could always swap all the values
temp = image[i][center-j]
image[i][center-j] = image[i][center-1+j]
image[i][center-1+j] = temp
But that is always three operations whereas the if statement is only 3 operations in the worst case.
Helpful Answer?
Yes |
No
Inappropriate?
For unsigned integers, the bitwise complement of a number is the "mirror reflection" of the number across the half-way point of the range of the unsigned integer type. For example, for 8-bit unsigned integers, ~x == 255 - x, which can be visualized on a graph, as a downward line, which effectively "flips" an increasing range from 0 to 255, to a decreasing range from 255 to 0. A simple use of this is that to invert the colors of a grayscale or RGB image where each color component for every pixel is stored as an unsigned integer, one simply needs to bitwise complement all these integers.
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
0 of 0 people found this helpful
by coder:
{
for (int i=0; i < size; i++) {
for (int j=0; j < size; j++) {
image[i][j] = !image[i][j];
}
}
}