HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Find a golden tile from a list of tiles

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
goldentilesfindlistfromtile

Problem

I had an interview and was asked a problem. I provided a solution and wanted to make sure if this is correct.

There is a Tile class. It has a boolean variable isGoldenTile which is false for all but true for one tile.

There is a List neighbors. These are list of its immediate neighbors which are tiles.

There is a method getNeighbors() which returns the list of neighbors.

Finally, there is a method findGoldenTile which returns a Tile object. This returns the golden tile.

In no way I could alter the class except for the method body for findGoldenTile.

Here is my solution:

public class Tile {

    private boolean isGoldenTile = false;   
    private List neighbors = null;

    public Tile(boolean isGoldenTile, List neighbors) {
        this.isGoldenTile = isGoldenTile;
        this.neighbors = neighbors;
    }

    public boolean isGoldenTile() {
        return isGoldenTile;
    }

    public List getNeighbors() {
        return neighbors;
    }

    public Tile findGoldenTile() {
        if(this.isGoldenTile == true){
            return this;
        } else if (this.getNeighbors().size() > 0){
            for(int i=0;i<this.getNeighbors().size();i++){
                if(this.getNeighbors().get(i).isGoldenTile == true){
                    return this.getNeighbors().get(i);
                }
            }
        } else {
            for(int i=0;i<this.getNeighbors().size();i++){
                return this.getNeighbors().get(i).findGoldenTile();
            }
        }
        return null;
    }
}

Solution

You need to keep track of visited tiles, or you risk ending in an infinite (recursive) loop. If you use recursion, and if you are not allowed to change the class, this is difficult, as you'd need to add another parameter to findGoldenTile holding the already visited tiles.

Alternatively, you could not make it recursive, but with a queue and with the set of visited tiles in the method itself. Something like this (not tested):

public Tile findGoldenTile() {
    Set visited = new HashSet<>();
    LinkedList queue = new LinkedList<>();
    queue.add(this);
    visited.add(this);
    while (! queue.isEmpty()) {
        Tile tile = queue.pop();
        if (tile.isGoldenTile) {
            return tile;
        }
        for (Tile x : tile.neighbors) {
            if (! visited.contains(x)) {
                queue.add(x);
                visited.add(x);
            }
        }
    }
    return null;
}

Code Snippets

public Tile findGoldenTile() {
    Set<Tile> visited = new HashSet<>();
    LinkedList<Tile> queue = new LinkedList<>();
    queue.add(this);
    visited.add(this);
    while (! queue.isEmpty()) {
        Tile tile = queue.pop();
        if (tile.isGoldenTile) {
            return tile;
        }
        for (Tile x : tile.neighbors) {
            if (! visited.contains(x)) {
                queue.add(x);
                visited.add(x);
            }
        }
    }
    return null;
}

Context

StackExchange Code Review Q#111244, answer score: 6

Revisions (0)

No revisions yet.