
Asked in Amazon
Fish Eater Problem Statement
In a river where water flows from left to right, there is a sequence of 'N' fishes each having different sizes and speeds. The sizes of these fishes are provided in an array called FISHES. Larger fishes swim faster and can eat smaller fishes they encounter. However, they cannot eat fishes of the same size. Determine how many fishes survive at the end of such encounters.
Input:
The input consists of multiple test cases. The first line contains an integer 'T' representing the number of test cases. Each test case consists of two lines. The first line has the integer 'N', the number of fishes. The second line has 'N' space-separated integers specifying the sizes of the fishes.
Output:
For every test case, return the number of fishes that survive.
Example:
Input:
N = 5
FISHES = [4, 2, 3, 1, 5]
Output:
2
Explanation:
Fish 4 will eat fish 2, leaving [4, 3, 1, 5].
Fish 3 will eat fish 1, leaving [4, 3, 5].
Fish 4 will eat fish 3, leaving [4, 5]. Since fish 5 cannot encounter fish 4, they both survive.
Constraints:
- 1 <= T <= 10
- 1 <= N <= 10^4
- 1 <= FISHES[i] <= 10^9
Note:
You do not need to print the result, the function should only compute and return the output.

AnswerBot
4mo
Given a river with fishes of different sizes and speeds, determine how many fishes survive after encounters.
Iterate through the array of fishes and simulate encounters between fishes to determine surv...read more

Saikumarreddy Palakolanu
5mo
public int function(int[] arr){ int count=0; int i=0; int j=1; while(j<arr.length){ if(arr[i]<arr[j]){ count++; i=j; } j++; } return count; }
Help your peers!
Add answer anonymously...
Top Software Developer Intern Interview Questions Asked at Amazon
Q. What is the code to calculate the distance between two nodes in a binary tree?
Q. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
Q. For a given array, how would you count the number of inversions?
Interview Questions Asked to Software Developer Intern at Other Companies
Top Skill-Based Questions for Amazon Software Developer Intern
C++ Interview Questions and Answers
300 Questions
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Operating Systems Interview Questions and Answers
250 Questions
System Design Interview Questions and Answers
250 Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

