Gvarun Rao

Greenhorn
+ Follow
since May 22, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
4
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Gvarun Rao

Hi,

I was recently asked a Java multi-threading question in an interview. The questions is : Write a program that creates 3 Threads. The first Thread prints 1, the second Thread prints 2 and the third 3. The Threads should run in such a way that they print the sequence 1 2 3 1 2 3 .... forever



I wrote code similar to what I have above and it works. However, I got the impression that the interviewer wasn't very happy with this solution. Any suggestions on how I can improve this program is greatly appreciated.

Thanks
Varun
8 years ago
Thanks for all the replies. I was able to fix my code based on the feedback I received:



This code prints:

3
Dishes chosen [5, 6, 7]
8 years ago
Hi,

people_fed should've been people_fed_by_d.

I was thinking that if dish d is selected then : [number of people to feed - number of people fed by dish d] will be remaining. The other option is to not pick dish d. Then, we still have : [number of people to feed] remaining.
Then I continued the recursion with dish d-1.

opt(d,people_to_feed)=Min(1+opt(d-1, people_to_feed - people_fed_by_d) + opt(d-1,people_to_feed))
8 years ago
Hi,


I came across the following question on careercup.com:


There are n persons and k different type of dishes. Each person has some preference for each dish. Either he likes it or not. We need to feed all people. Every person should get at least one dish of his choice. What is the minimum number of different type of dishes we can order?

Input is n x k matrix boolean matrix.For each person a row represent his likes or not likes each row.

n = 6 k = 7
1 0 0 0 1 0 0
1 0 0 0 0 1 0
1 0 0 0 0 0 1
0 1 0 0 1 0 0
0 0 1 0 0 1 0
0 0 0 1 0 0 1

Output
3

Explanation
Take dish number 5,6,7.



I was able to see that greedy approach would give me an incorrect answer of 4 (select dishes that are favorites of maximum number of people). However, I feel that dynamic programming approach would be apt in this case. So, I'm thinking in order to solve the main problem, it can be thought of as making a choice of whether to select a particular dish or not.

I think the following recursion can be used to determine the solution

opt(d,people_to_feed)=Min(1+opt(d-1, people_to_feed - people_fed) + opt(d-1,people_to_feed))

where d is a dish number

I wrote the following code



The result comes back as 0 instead of 3.

Please let me know if there are some modification with which this approach can produce the correct result.

Thanks
Varun
8 years ago
Stefan,

Your suggestion about using the order is spot-on. Thank you for your help.

I used an additional parameter to keep track of the item/coin that I previously used. I am posting the full code below, to help others who want to know how I solved the issue.


This gives the correct 13 combinations below:


25s 1 10s 0 5s 0 1s 0 order 25
25s 0 10s 2 5s 1 1s 0 order 10 10 5
25s 0 10s 2 5s 0 1s 5 order 10 10 1 1 1 1 1
25s 0 10s 1 5s 3 1s 0 order 10 5 5 5
25s 0 10s 1 5s 2 1s 5 order 10 5 5 1 1 1 1 1
25s 0 10s 1 5s 1 1s 10 order 10 5 1 1 1 1 1 1 1 1 1 1
25s 0 10s 1 5s 0 1s 15 order 10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
25s 0 10s 0 5s 5 1s 0 order 5 5 5 5 5
25s 0 10s 0 5s 4 1s 5 order 5 5 5 5 1 1 1 1 1
25s 0 10s 0 5s 3 1s 10 order 5 5 5 1 1 1 1 1 1 1 1 1 1
25s 0 10s 0 5s 2 1s 15 order 5 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
25s 0 10s 0 5s 1 1s 20 order 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
25s 0 10s 0 5s 0 1s 25 order 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1



Thanks
Varun
8 years ago
Hi,

I am currently preparing for a technical interview. I've been solving questions from the book "Cracking the coding interview". I am currently stuck on one of the problems.

Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent), write code to calculate the number of ways of representing n cents.



I know that the question asks for the different combinations of the coins that make up the value = n. But, I wanted to test my understanding of recursion and wrote some code to print all possible combintions in the solution.



When I saw the output I realized my mistake. The algorithm uses the logic that makes it see two quarters as different and two dimes as different and so on.

While brainstorming to fix this issue, I thought of using a Set to remove duplicates but still seems wasteful as I should not even be generating them.

Any pointers on how I could fix this algorithm are greatly appreciated.

Thanks
Varun
8 years ago
Thanks Alexey, Seetharaman and Rob.
@Seetharaman : I am able to get a good estimate by using your method

@Rob:Can you tell me if I am using your method correctly because I am not arriving at the right answer.

12 years ago
Thanks Seetharaman but I don't think the "-" operator can be applied to two Date object operands.
12 years ago
Hi,
I'm trying to write a program that computes the number of days between two dates. Can anyone suggest how i can achieve this. I'm quite sure I'm supposed to use the Calendar class. Thanks in advance.
12 years ago
Hi everyone,
Yesterday I took the OCJP 1.6 and I am pleased to inform you that I secured 91%.
Thanks Kathy Sierra and Bert Bates, your book helped a lot.
Thanks JavaRanch members for your help.

Varun
12 years ago
Thanks Udara. Your help is much appriciated.
12 years ago
Thanks Matthew. I rewrote the above code as follows. I have synchronized on any instance of the StringBuffer class so that the threads can lock onto instances of the StringBuffer which they all use. Now the program prints unbroken lines of As,Bs and Cs but the order in which they are printed is not fixed. How can I fix it such that As,Bs and Cs are printed in order.

12 years ago
Hi everyone,
I am preparing for the SCJP test and I have trouble understanding the usage of the synchronized keyword. In the following code i am attempting to create three threads and synchronize the run method such that 100 As Bs and Cs will be printed unbroken on one line. But the trouble is that the output is getting all jumbled up. Am I right in thinking that since the run method is synchronized, once a thread has entered it, no other thread can enter until the first thread had exited the method. Can anyone please tell me where I'm going wrong.
Thanks in advance.

12 years ago