aspose file tools*
The moose likes Java in General and the fly likes Core Java Real time based questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Core Java Real time based questions" Watch "Core Java Real time based questions" New topic
Author

Core Java Real time based questions

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi ranchers,
I have below Core Java Real time based questions

I attended a interview and was asked below set of qns for data structures and algorithms in java







When The Going Gets Tougher,The Tougher gets Going
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

Deepak Lal wrote:
I attended a interview and was asked below set of qns for data structures and algorithms in java


What were your answers? We can give you hints in the right direction (or confirm that you were correct), if you like.

Henry


PS... BTW, I think you meant "real world" based questions. "realtime" has a very specific meaning in computing, and its not what you are asking here.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Henry Wong wrote:
Deepak Lal wrote:
I attended a interview and was asked below set of qns for data structures and algorithms in java

What were your answers? We can give you hints in the right direction (or confirm that you were correct), if you like.
Henry
PS... BTW, I think you meant "real world" based questions. "realtime" has a very specific meaning in computing, and its not what you are asking here.


@Henry,I do not know the answers therefore im posting the questions here.If i dont know the answers then what kind of hints can you give me.
@Henry and other Ranchers,It would be pointless to ask you people or post in such forums if i knew the answers. why should i be doing that ???
These are just questions for me.probably you can interpret them as "Just Core Java Questions"
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

Deepak Lal wrote:
@Henry,I do not know the answers therefore im posting the questions here.If i dont know the answers then what kind of hints can you give me.


I can do better than a hint, I can tell you exactly what to do.

This is a interview question. It is also not a multiple choice question, not a question with a one word answer, and not even a question with the short answer.

Never, ever, and I mean, absolutely never, say that you don't know the answer during the interview -- even if you don't know the answer. The reason the question is asked is not to test whether you know the answer, but how you think through a problem.

Go to the board, draw out the problem, brainstorm the problem. And by brainstorm, I mean brainstorm out loud -- make sure the interviewer knows what you are thinking, and how you are approaching the problem. etc. Also, don't be afraid to ask the interviewer for clarification about the problem, during this process -- it demonstrates your thinking (or knowing) when you don't have enough information, and your willingness to go back and get more information from the stakeholders.

Henry
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

1> Linked List data structure should be my answer for Phone book application.but how to search by name and number.correct me if I am wrong
2> i do not know the approach of finding duplicates in 10,000 elements effeciently,except that the fact there is a solution of comparing each element with next one with a complexity if O(n2),but i need an a effecient solution.Did you have a better approach?
3>I really don't know the answer for this.Please help me for this.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

For duplicates in 10,000 strings I would write this following code, this code finds duplicates and sorts in ascending order. I don't know whether this is right approach or not.
>


Love all, trust a few, do wrong to none.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Algorithm for phone book, take a SortedMap, put all the corresponding key and value pairs in it.

Get the mobile number using map.get("name");

Problem with maps is we can get the value using key, reverse is somewhat difficult. For this get all the keys in the SortedMap into an Object, then compare each and every value with the search value, if returned true, then print the Object value, i.e. the key.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

chaitanya, please don't provide full solutions. LetThemDoTheirOwnHomework. For what it's worth, your code looked pretty good to me. I just would have used a LinkedHashMap instead of a TreeMap to a) preserve order, and b) have slightly faster lookups.

As for the name to number lookup, your solution would only have worked for exact matching. If I would search for "Rob" I wouldn't find my own number, whereas I would like to find both my number and that of other Robs around.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

[quote=Rob Prime
As for the name to number lookup, your solution would only have worked for exact matching. If I would search for "Rob" I wouldn't find my own number, whereas I would like to find both my number and that of other Robs around.

Thanks Rob for the suggestions, I forgot to mention two things, one is exact matching which you said, and other is what if, if any one put the same value with different key.

I left these things to Deepak only.
chaitanya, please don't provide full solutions. LetThemDoTheirOwnHomework.

Actually I took it as a homework, I too was waiting for someone to judge whether the approaches are right or wrong and sorry, I don't know about this LetThemDoTheirOwnHomework.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13863
    
  10

Deepak Lal wrote:1> Linked List data structure should be my answer for Phone book application.but how to search by name and number.correct me if I am wrong

Can you explain why you would choose a linked list? What alternatives are there to a linked list? What are the advantages and disadvantages of a linked list compared to other data structures?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Core Java Real time based questions
 
Similar Threads
Sort key value pair by ordered array of keys
Garbage collection and memory leaks question
Searching through an array to find and print duplicate random numbers
Sun Cirtification
I can NOT compile first class below