Amazon Interview Question
Software Engineer in TestsCountry: India
Interview Type: Written Test
This logic is same as above.
void fill_color_impl(int** pixmap, int width, int height, int x, int y, int fillColor, int currentColor, int currentColorUndefined) {
if (x < 0 || x >= width ||
y < 0 || y >= height)
return;
if (currentColorUndefined) {
currentColor = pixmap[y][x];
currentColorUndefined = 0;
}
if (currentColor != pixmap[y][x])
return;
pixmap[y][x] = fillColor;
fill_color_impl(pixmap, width, height, x-1, y, fillColor, currentColor, currentColorUndefined);
fill_color_impl(pixmap, width, height, x, y-1, fillColor, currentColor, currentColorUndefined);
fill_color_impl(pixmap, width, height, x+1, y, fillColor, currentColor, currentColorUndefined);
fill_color_impl(pixmap, width, height, x, y+1, fillColor, currentColor, currentColorUndefined);
}
void fill_color(int** pixmap, int width, int height, int x, int y, int fillColor) {
fill_color_impl(pixmap, width, height, x, y, fillColor, 0, 1);
}
This logic is same as above.
void fill_color_impl(int** pixmap, int width, int height, int x, int y, int fillColor, int currentColor, int currentColorUndefined) {
if (x < 0 || x >= width ||
y < 0 || y >= height)
return;
if (currentColorUndefined) {
currentColor = pixmap[y][x];
currentColorUndefined = 0;
}
if (currentColor != pixmap[y][x])
return;
pixmap[y][x] = fillColor;
fill_color_impl(pixmap, width, height, x-1, y, fillColor, currentColor, currentColorUndefined);
fill_color_impl(pixmap, width, height, x, y-1, fillColor, currentColor, currentColorUndefined);
fill_color_impl(pixmap, width, height, x+1, y, fillColor, currentColor, currentColorUndefined);
fill_color_impl(pixmap, width, height, x, y+1, fillColor, currentColor, currentColorUndefined);
}
void fill_color(int** pixmap, int width, int height, int x, int y, int fillColor) {
fill_color_impl(pixmap, width, height, x, y, fillColor, 0, 1);
}
This logic is same as above.
void fill_color_impl(int** pixmap, int width, int height, int x, int y, int fillColor, int currentColor, int currentColorUndefined) {
if (x < 0 || x >= width ||
y < 0 || y >= height)
return;
if (currentColorUndefined) {
currentColor = pixmap[y][x];
currentColorUndefined = 0;
}
if (currentColor != pixmap[y][x])
return;
pixmap[y][x] = fillColor;
fill_color_impl(pixmap, width, height, x-1, y, fillColor, currentColor, currentColorUndefined);
fill_color_impl(pixmap, width, height, x, y-1, fillColor, currentColor, currentColorUndefined);
fill_color_impl(pixmap, width, height, x+1, y, fillColor, currentColor, currentColorUndefined);
fill_color_impl(pixmap, width, height, x, y+1, fillColor, currentColor, currentColorUndefined);
}
void fill_color(int** pixmap, int width, int height, int x, int y, int fillColor) {
fill_color_impl(pixmap, width, height, x, y, fillColor, 0, 1);
}
it will quickly overflow for large screen if using DFS. A BFS implementation though a little bit complex.
{{
enum Color {
BLACK, WHITE, RED, YELLOW, GREEN
}
public void paintfill(Color[][] screen, int x, int y, Color ocolor,
Color ncolor) {
Queue<Position> queue = new LinkedList<Position>();
queue.add(new Position(y, x));
while (!queue.isEmpty()) {
Position p = queue.remove();
if (fill(screen, p.x - 1, p.y, ocolor, ncolor))
queue.add(new Position(p.x - 1, p.y));
if (fill(screen, p.x + 1, p.y, ocolor, ncolor))
queue.add(new Position(p.x + 1, p.y));
if (fill(screen, p.x, p.y - 1, ocolor, ncolor))
queue.add(new Position(p.x, p.y - 1));
if (fill(screen, p.x, p.y + 1, ocolor, ncolor))
queue.add(new Position(p.x, p.y + 1));
}
}
public boolean fill(Color[][] screen, int x, int y, Color ocolor,
Color ncolor) {
if (x < 0 || y < 0 || x >= screen[0].length || y >= screen.length)
return false;
if (screen[y][x] == ocolor) {
screen[y][x] = ncolor;
return true;
} else
return false;
}
public static class Position {
public int x;
public int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
}}}
Pseudocode for fill color.
- anjum February 05, 2012void fill_color(int x,int y,int fillcolor,int bordercolor)
{ int currcolor;
currcolor=findcolor(x,y);
if((currcolor!=bordercolor)&(currcolor!=fillcolor))
{
setpixel(x,y,currcolor);
fill_color(x+1,y,fillcolor,bordercolor);//right
fill_color(x-1,y,fillcolor,bordercolor);//left
fill_color(x,y+1,fillcolor,bordercolor);//up
fill_color(x,y-1,fillcolor,bordercolor);//down
}
}