File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes To print MAX and MIN line from a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "To print MAX and MIN line from a text file" Watch "To print MAX and MIN line from a text file" New topic
Author

To print MAX and MIN line from a text file

Rakesh shankar
Greenhorn

Joined: Nov 12, 2010
Posts: 14
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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Rakesh shankar wrote:... Please explain me the logic. Can we make use any collection here? ...

You need to learn to reason these things out yourself. So please start by telling us what you think the logic should be, and we will help you refine it. For example, you suggested using a collection. How do you think that might help? What are you hoping to get from that?
kumarjit banerjee
Ranch Hand

Joined: Mar 27, 2011
Posts: 32

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



You may try the below code






Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29261
    
140

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
kumarjit banerjee
Ranch Hand

Joined: Mar 27, 2011
Posts: 32

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.



Hi Jeanne,

I agree with you that the code has turned out to be a bit complicated. But according to Rakesh if the following three lines as for example exists in the text file then

"Hi" is the min value and "This is a test line." is the max value for the below text in the file.

Hi
Hello
This is a test line.

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. For this reason the if-else blocks have increased to some extent. The second reason is for formatting the output as mentioned by Rakesh. If I would have printed the maps directly then 40 lines of code may be reduced.

Yes I agree that instead of creating helper objects it may be implemented by array of String and int, but in that case we have to traverse through the whole array. 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.

Please correct me if I am wrong and if array implementation will be more time effective.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29261
    
140

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.

I would ask the interviewer whether this was needed before overcomplicated things. They may just want a simple solution that prints the first one if multiple are found.

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.

Collections.sort() is not faster than traversing an array once. The Quicksort algorithm is n * logn or n *n in complexity. Going through a list once is n in complexity.

kumarjit banerjee wrote:Yes I agree that instead of creating helper objects it may be implemented by array of String and int

I wouldn't use an array either. I would do the work in the main loop as I go through the array. This is because it is an interview question and the whole thing can be done simply in a small amount of code. In the real world, I'd use an existing API that reads a file into an ArrayList<String> of String[] and loop through it once. Even there I don't have helper objects.

I don't understand where the need for helper objects comes from. I need to keep track of the max # characters, the min # characters and the lines that go with each. This sounds like four local variables to me. (Even if you want to support multiple lines of the same length this is the case; just with a different type for the local variables.)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Also, although the Collections.sort() API does specify what algorithm it uses, it's NOT Quicksort.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To print MAX and MIN line from a text file
 
Similar Threads
boundary value testing..max & min
Need to Print out put to Dialog box
Pyramid Printing Problem
Suggestions on database
Random function