Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

scan the arrays, when encountering downstream fish (B[i]==1) push the fish into a stack. Otherwise (upstream fish), compare the top of the stack to the current fish, and if the current fish is larger, pop the top of the stack, otherwise increase the "eaten" count by 1. Simple O(N) time & space.

- gen-y-s March 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is pseudo code I cooked up...

fishes_alive	= 0;
fish_upstream	= (dequeue(b) == 0);
fish_size 	= dequeue(a);

while (q_not_empty(b)) {

	if (fish_upstream) {
		fishes_alive++;
		fish_upstream	= dequeue(b);
		fish_size	= dequeue(a);
	} else {
		// fish is going downstream...check if it can eat anything else.
		next_fish_upstream 	= peek(b);
		next_fish_size		= peek(a);

		if (fish_upstream == next_fish_upstream) {
			// both fishes int the same direction...no need to compare sizes
			fishes_alive++;
			fish_upstream	= dequeue(b);
			fish_size	= dequeue(a);
		} else {
			// next fish is upstream, curr fish is downstream....compare sizes
			if (fish_size < next_fish_size) {
				// fish gets eaten by next fish
				fish_upstream = dequeue(b);
				fish_size     = dequeue(a);
			} else {
				// next fish gets eaten by current fish
				dequeue(b);
				dequeue(a);	// not storing return value => fish dies
			}
		}				
			
	}
}

if (fish_upstream != true) {
	// we had a downstream fish and it gobble up all the way to the end
	// i.e. it is alive and we need to count it
	fishes_alive++;
}

return (fishes_alive);

- smallchallenges March 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

small-changes you need to use a stack...... go away with your wrong solutions.....

- gen-x-s March 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code in python based on my answer above:

def countFish(a, b):
  l=list()
  n=len(a)
  cnt=0

  for i in range(n):
    if b[i]==1:
      l.append(a[i])
    elif len(l)>0:
      n-=1
      if l[-1]<a[i]:
        l.pop()

  return n

def main():
  a=[4,3,2,1,5]
  b=[0,1,0,0,0]
  print(a,b)
  print(countFish(a,b))

if __name__ == "__main__":
  main()

- gen-y-s March 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.mohit.stack;

import java.util.Stack;

public class fish {

    private Stack<Integer> stack = null;
    
    public fish() {
        int[] A = new int[5];
        int[] B = new int[5];
        A[0] = 4;    B[0] = 1;
        A[1] = 3;    B[1] = 0;
        A[2] = 2 ;   B[2] = 0;
        A[3] = 10  ;  B[3] = 1;
        A[4] = 5   ; B[4] = 0;
        
        System.out.println(solution(A,B));
    }

    public int solution(int[] A, int[] B) {
        int result = 0;
        if (A.length == 0 || B.length == 0)
            result = 0;

        stack = new Stack<Integer>();
        checkAndPush(A, B);
        System.out.println("input = " + A.length + " and result = " + stack.size());
        return stack.size();
    }

    private void checkAndPush(int[] A, int[] B) {

        for (int i = 0; i < B.length; i++) {

            stack.push(i);
            while (stack.size() > 1) {
                int pop = stack.pop();
                int peek = stack.peek();

                if (B[pop] == 0 && B[peek] == 1) {
                    if (A[pop] > A[peek]) {
                        stack.pop();
                        stack.push(pop);
                    }
                } else {
                    stack.push(pop);
                    break;
                }
            }
        }

    }

}

- Mohit March 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.mohit.stack;

import java.util.Stack;

public class fish {

    private Stack<Integer> stack = null;
    
    public fish() {
        int[] A = new int[5];
        int[] B = new int[5];
        A[0] = 4;    B[0] = 1;
        A[1] = 3;    B[1] = 0;
        A[2] = 2 ;   B[2] = 0;
        A[3] = 10  ;  B[3] = 1;
        A[4] = 5   ; B[4] = 0;
        
        System.out.println(solution(A,B));
    }

    public int solution(int[] A, int[] B) {
        int result = 0;
        if (A.length == 0 || B.length == 0)
            result = 0;

        stack = new Stack<Integer>();
        checkAndPush(A, B);
        System.out.println("input = " + A.length + " and result = " + stack.size());
        return stack.size();
    }

    private void checkAndPush(int[] A, int[] B) {

        for (int i = 0; i < B.length; i++) {

            stack.push(i);
            while (stack.size() > 1) {
                int pop = stack.pop();
                int peek = stack.peek();

                if (B[pop] == 0 && B[peek] == 1) {
                    if (A[pop] > A[peek]) {
                        stack.pop();
                        stack.push(pop);
                    }
                } else {
                    stack.push(pop);
                    break;
                }
            }
        }

    }

}

- Mohit March 26, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More