Sunday, December 20, 2015

Daily Hunt: Interview Questions #3

As I have been part of rat race to find another Job. It was my 3rd interview with Daily Hunt.. It was mix of data structures and Java. I was asked 3 questions on Data Structures and various other questions related to Java.

Question 1: It is related to Data Structure. You have been given list of integers in an array and an another integer X. It is known that multiplication of 2 integers in the array is equal to X. Find the numbers from the array.

Solution 1: Solution I provided was to have 2 separate arrays. One contains the input as received and another to keep integers as Binary Search Tree.

We will iterate through the array to find the first multiplier, and once we found it we will try to find the remaining multiplier in the BST array. If the number is found is the BST, we found the else otherwise move to another integer in the input array.

Question 2: This question is on the design patterns. Design the coffee vending machine?
Coffee Vending machine is simple machine which are part of offices now a day. If you have to implement the coffee building machine, how would you do it?

Coffee vending machine can have options for various tea and various coffees. So what would be the best approach to design a software for such a coffee vending machine.

Now we have various design patterns available with us. What would be the best one for you?

The answer for the problem is the Decorator Pattern. We would discuss the same in the upcoming post in the details. Though I answered it with Abstract Factory and Abstract Method pattern. :-(

Question 3: Interviewer again moves to DS. An application is receiving the stream of strings. String may contain anagram. Application would need to detect those anagrams in the stream.
It is required to check the stream again to figure out if application has already received the string earlier in the stream to report it to be duplicate. But based on the question, 2 strings are duplicate when both of them are anagram to each other.

Simple answer for the question would be to read the string from the stream and sort the character string. Save the string in the Set, to maintain unique strings. If application tries to add the string in the set and it is already added to the set, set does not add it to array and returns false. In this case, the string can be marked as DUPLICATE.

Let's look at the example:
String a is not duplicate of ton of trings.
[String] => Set will contain {ginrst}
[a] => Set {ginrst, a}
[is] => Set {ginrst, a, is}
[not] => Set {ginrst, a, is, not}
[duplicate] => Set {ginrst, a, is, not, acdeilptu}
[of] => Set {ginrst, a, is, not, acdeilptu, of}
[ton] => Set {ginrst, a, is, not, acdeilptu, of} : No addition as 'not' already exists. It is considered as duplicate string.
[of] => Set {ginrst, a, is, not, acdeilptu, of} : No addition as 'of' already exists. It is considered as duplicate string.
[trings] => Set {ginrst, a, is, not, acdeilptu, of} : No addition as 'ginrst' already exists.

Question 4: So here comes a puzzle. 2 persons are landing on a straight line on a point A and B. Points A and B are not known to each of them. How would they find each other?
This one was interesting puzzle. I did not really have any clue about it. So I requested for the clue. Interviewer said that both the person can move in any direction. But it indicated that one of these two would move like a pendulum. So let's say that A moves but not B. So A moves to his left for half KM and later he moves to his right for 1Km. Next if could not find B yet, he moves to his left for 1.5KM, and so on.

Question 5: Here comes a DB cum Java question. An application has one table which contains only one column and one row. It maintains the user id of the users who are registering to the application. Database can only cater 10 update request per second, but number of user registering each second are 1000. How would you tackle the deadlock in this situation?
Now this was a tricky question. Initially I thought that it is required to look the solution at DB side, but rather the problem could have been solved at java side as well. My first thought was to think in terms of triggers. But triggers can not be the solution for this. Why did I think so? Trigger is a DB operation which will increase the count and save the count to database and return the value. In addition, triggers values are cached as well. It can also be a solution.

Java side we can fire the DB update after certain number of users have been created. So it is possible to maintain a static variable which will have the latest value.

I am now thinking, can we even use volatile or threadlocal variable in this? Please let me know if you have some better answer.

Question 6: What is serialization? Why do we use serialized version id for each class?
Serialization is to persist the Java memory object into file system in the form of file or database. readObject and writeObject methods are used to deserial and serial the object into the file file system.

To achieve serialization, it is required for class to implement Serializable interface.

serialVersionID is maintained to differentiate between 2 different version of classes. It helps loader to identify the class and deserial the specific object based on the version of the class. Serialization has been explained quite well in this blog.
http://exploreriddles.blogspot.in/2014/01/serialization-introduction.html

Question 7: On the deployed tomcat server, you have detected memory leak. How would you find the memory leak?
Okay, I assume that there is no fix to find the memory leak. In this case, we would need to first check in our application if something is possible leak. If it is not, it would be required to fetch the memory profile of the tomcat application and run through the profile to figure out the actual reason for the memory leak.
<!-- Notes to Me --> Learn more about Profiling.

Question 8: An application has a thread pool for 10 threads, but tasks provided to the threads are more than 10. How would you handle the situation programmatically?
Okay, I could not handle last 3 questions quite well. I was not to answer all simple questions of this interview. My assumption was that threadpool handles this scenario at it's own. As soon as, we submit a task to thread pool, it checks if there is any thread which is free. Threadpool assigns the task to the certain thread else if all threads are running, it send the task in the wait status to be notified once ny of the thread becomes free.

I am not an expert. I am also for the solution for the questions stated above. If you know some answers, let me know.

No comments:

Post a Comment