Rakesh shankar wrote:... Please explain me the logic. Can we make use any collection here? ...
Rakesh shankar wrote:Hi all,
Recently I have attended one of the interview. I got this question.
Question: Write a Java program to print max and min line in a text file. Also print the line which has max & min characters.
Example:
Hi
Hello
This is a test line.
Output should be
Max line : Line number 3 "This is a test line"
Min line : Line number 1 "Hi"
Please explain me the logic. Can we make use any collection here?
Thanks in advance
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote:Rakesh,
The point of such an interview question is to show you can write a simple algorithm. Forget about Java. If you were doing this by hand, what would you do? What would you write down or keep track of in your head? Try explaining it here and we can help you clarify your ideas.
kumarjit,
Can you think of a simpler way to do it? That approach is much more complicated than it needs to be. I'm not clear on why you need the Collections.sort or helper objects to solve the problem.
kumarjit banerjee wrote:But however I also considered that if "Hi" occurs twice or "Ha" occurs which consists of the same number of characters, then both the line numbers should be displayed. So is true for max value. If a blank line is encountered then depending upon the argument value true or false it should be ignored or considered.
kumarjit banerjee wrote:So I chose Collections.sort() which uses the quick sort algorithm so is faster than traversing the whole array. But it obviously involves an overhead of creating the helper objects.
kumarjit banerjee wrote:Yes I agree that instead of creating helper objects it may be implemented by array of String and int
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
The fastest and most reliable components of any system are those that are not there. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|