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

Find biggest basin

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

Problem

Problem Statement


A group of farmers has some elevation data, and we’re going to help
them understand how rainfall flows over their farmland.


We’ll represent the land as a two-dimensional array of altitudes and
use the following model, based on the idea that water flows downhill:


If a cell’s eight neighboring cells all have higher altitudes, we call
this cell a basin; water collects in basin.


Otherwise, water will flow to the neighboring cell with the lowest
altitude.


Cells that drain into the same sink – directly or indirectly – are
said to be part of the same basin.


A few examples are below:

-----------------------------------------
Input:                 Output: 

 1 1 2                 1 4 ( basin is 1, and size is 4)
 1 1 7 
 3 6 9


Looking for code review optimizations and best practices.
Complexity - both time and space is O(n*m)

```
final class BasinData {

private final int item;
private final int count;

public BasinData(int item, int count) {
this.item = item;
this.count = count;
}

public int getItem() {
return item;
}

public int getCount() {
return count;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + count;
result = prime * result + item;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BasinData other = (BasinData) obj;
if (count != other.count)
return false;
if (item != other.item)
return false;
return true;
}
}

/**
* References:
* http://www.geeksforgeeks.org/flipkart-interview-set-2-for-sde-1/
*
* Complexity:
* O(n2)
*/
public final class Basin {

private Basin(

Solution

I find this a nice piece of code.

Still I found 1 minor and 1 bigger issue.

Bigger issue :

You say in the javadoc that if more then 1 bassin is found the biggest must be returned.

You implement nice testing but this I don't find back in the testings.

All your testings have 1 lowest bassin.

I should add this to the test :

@Test
public void testSingleElementBasin() {
    int[][] m4 = { {1, 0, 0, 1},
                   {1, 0, 3, 1},
                   {4, 5, 6, 0} };
    assertEquals(new BasinData(0, 3), Basin.getMaxBasin(m4));
}


Minor :

I should refactor the public static BasinData getMaxBasin(int[][] m)

public static BasinData getMaxBasin(int[][] m) {
    final List basinCountList = getBassinCountList(m);
    return getMaxBassin(basinCountList);
}


You put the space because you know you are doing 2 different things, just do that extra step to refactor to 2 methods.

Code Snippets

@Test
public void testSingleElementBasin() {
    int[][] m4 = { {1, 0, 0, 1},
                   {1, 0, 3, 1},
                   {4, 5, 6, 0} };
    assertEquals(new BasinData(0, 3), Basin.getMaxBasin(m4));
}
public static BasinData getMaxBasin(int[][] m) {
    final List<BasinCount> basinCountList = getBassinCountList(m);
    return getMaxBassin(basinCountList);
}

Context

StackExchange Code Review Q#49048, answer score: 10

Revisions (0)

No revisions yet.