# Fish – Codility – Solution

Java solution to Codility Fish problem (Lesson 7 – Stacks and Queues) which scored 100%. The problem is to determine how many fish are alive in a river of fish moving upstream and downstream.

The strategy is to keep a running stack of fish as go through array of fish swimming in same direction or upstream until encounter fish going in opposite direction and then figure out what fish will survive.

 1234567891011121314151617181920212223242526272829303132333435363738 package com.codility.lesson07.stacksqueues; import java.util.Stack; public class Fish {   public int solution(int[] A, int[] B) {     final int UPSTREAM = 0;         Stack fishStack = new Stack();     fishStack.push(new Fish1(A[0], B[0]));     for(int i=1; i A[i]) {               break; //eating finished             }             else {               fishStack.pop();               continue; //keep checking other elements on stack             }           }         }         if(fishStack.isEmpty()) {           fishStack.push(new Fish1(A[i], B[i])); //current fish ate all the fish in the stack         }       }     }     return fishStack.size();   }     class Fish1 {     private int size;     private int direction;         Fish1(int pSize, int pDirection) {       size = pSize;       direction = pDirection;     }   } }

TestNG test cases for this problem which all passed:

 12345678910111213141516171819202122232425262728293031323334353637 package test.com.codility.lesson07.stacksqueues; import org.testng.Assert; import org.testng.annotations.BeforeTest; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import com.codility.lesson07.stacksqueues.Fish; public class FishTests {   private Fish solution;     @BeforeTest   public void setUp() {     solution = new Fish();   }   @DataProvider(name = "test1")   public Object [][] createData1() {     return new Object [][] {       new Object [] {  new int [] { 4, 3, 2, 1, 5 }, new int [] { 1, 0, 1, 0, 1 },  3 },       new Object [] {  new int [] { 4, 3, 2, 0, 5 }, new int [] { 0, 1, 0, 0, 0 },  2 },       new Object [] {  new int [] { 4, 3, 2, 1, 5 }, new int [] { 0, 1, 0, 0, 0 },  2 },       new Object [] {  new int [] { 4, 3, 2, 1, 5 }, new int [] { 0, 1, 1, 0, 0 },  2 },       new Object [] {  new int [] { 4, 3, 2, 5, 6 }, new int [] { 1, 0, 1, 0, 1 },  2 },       new Object [] {  new int [] { 7, 4, 3, 2, 5, 6 }, new int [] { 0, 1, 1, 1, 0, 1 },  3 },       new Object [] {  new int [] { 3, 4, 2, 1, 5 }, new int [] { 1, 0, 0, 0, 0 },  4 },       new Object [] {  new int [] { 3 }, new int [] { 1 },  1 },       new Object [] {  new int [] { 3 }, new int [] { 0 },  1 },     };   }   @Test(dataProvider = "test1")   public void verifySolution(int [] pA, int [] pB, int pExpected) {       Assert.assertEquals(solution.solution(pA, pB), pExpected);   } }