This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To print MAX and MIN line from a text file

 
Rakesh shankar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, although the Collections.sort() API does specify what algorithm it uses, it's NOT Quicksort.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic