In this post, I have come with another simple programming problems for Java beginners. I love to share short programming problems because they help in developing programming sense. Many people will argue against simple problems like
prime numbers,
palindrome and
factorial, but I really find them useful, especially for beginners. A beginner is far away to solve a complex data structure problem or even more complex problems like those appear in
TopCoder or other programming sites. Programmers learn gradually and they need joy of doing something and seeing result much quickly then any other. Small success motivates them. Anyway, here is our problem statement, you need to write a Java program to
find top two maximum numbers in the given array. You can not use any sorting functions and you should iterate the array only once. Use of any kind of collection class e.g.
TreeSet or LinkedHashSet are also not allowed. For example, if given integer array is
[20, 34, 21, 87, 92, 2147483647] then first maximum is
2147483647 and second maximum is
92. Bonus points are for those, who can also write
JUnit test cases, more test scenarios, more points. Few more bonus points for those who can write code to deal with really large array, something which may not entirely fit on memory.