• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

To print MAX and MIN line from a text file

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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






 
author & internet detective
Posts: 40801
829
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 40801
829
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Master Rancher
Posts: 4062
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, although the Collections.sort() API does specify what algorithm it uses, it's NOT Quicksort.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 74384
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is really poor quality code, which would obviate any risk of getting the job if supplied during an interview. I shall leave you to work out what is wrong with it.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic