A while ago, I came across an interesting problem. We have 3 different sorted queues (entries were sorted by time), out of which we had to pickup the smallest time, and send the first arrived entry out of those 3 queues in sequence.
Q1
Q2
Q3
2
3
1
5
4
6
7
6
9
8
10
11
As you can see, all queues (Q1, Q2, Q3) are already sorted, but on application side we have to pickup entries across all queues in sorted order and process them. So the entries should be processed in 1, 2, 3, 4, 5, … 11 sequence. Frankly this is merging of already sorted lists, and a person from computer science background should already know this. But I don’t come from such background, and the solution one of my friends gave was plain amazing.
The solution was pickup first entry from each queue, compare them, choose the smallest and process it first.
Q1
Q2
Q3
Smallest
2
3
1
1
Thus, here 1 is processed first. Now pickup next entry from queue where smallest was found (Q3 in this example), so now the next comparison becomes like this
Q1
Q2
Q3
Smallest
2
3
6
2
The smallest is 2, so this is processed and next entry from Q1 was picked-up
Q1
Q2
Q3
Smallest
5
3
6
3
And so on… giving us
Q1
Q2
Q3
Smallest
2
3
1
1
2
3
6
2
5
3
6
3
5
4
6
4
5
6
6
5
7
6
6
6
7
10
6
6
7
10
9
7
8
10
9
8
10
9
9
10
11
10
11
11
A simple, and elegant solution, isn’t it? Is there any other better alternative?
How many degrees angle between hour, minute hand when time is 3:15?
7.5°
360° full circle, therfore (360°/12 hrs) = 30° /hour.
At 3:15, minutes hand points to the 3, hour hand is ahead of 3 by 1/4th (15/60), which is 7.5° apart. (30°/4)=7.5°
Why are manhole covers round?
– Round covers can be easily rotated
– It’s easier to dig a circular hole
– Round castings are easier to machine using a lathe
– Easier to manufacture than custom-made covers
How will you generate random number between two given numbers
Assuming n1, n2 random number between them would be n1 + Math.random() * (n2 – n1)
How will you sort a million integers?
Split them into meaningful subsets, sort each subset, merge subset back
http://neopythonic.blogspot.com/2008/10/sorting-million-32-bit-integers-in-2mb.html
How many windows will be open in a 100 window room if you go around a 100 times toggling each window you come across? All windows are closed initially.
100 windows, all closed.
1st round, you toggle all windows i.e. all windows are opened
2nd round, you close all even-numbered windows – 2, 4, 6, …
3rd round, you open, close odd numbered winodws – 3, 6, 9,…
So after 100 times, windows open are windows who have been toggled odd number of times. For e.g. consider window 12, round 1 will make it open, 2 will make it closed, 3 will make it open, 4 closed, then directly 12 will make it open. Consider again, window 20 – 1 (open), 2 (close), 4 (open), 5 (close), 10 (open), 20 (close). Consider 7 – 1 (open), 7 (close). So window 12 got touched 5 times, window 20 got touched 6 times, window 7 touched 2 times.
So, in order to find “open” windows between 1-100, we have to find number having “odd” number of factors, which means numbers who have one repeated factor i.e. factor by same factor gives the number e.g. 16 has 1, 2, 4, 8, 16 – open window. It has 4 which means 4×4= 16. Consider 20 has 1, 2, 4, 5, 10, 20. No factor is repetitive.
Therefore, let us find numbers which are below 100, but are having square products, similar to 4×4 = 16 starting with 1, which gives 12=1, 22=4, 32=9, 42=16, 52=25, 62=36, 72=49, 82=64, 92=81, 102=100
So open windows are 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 = 1o open windows
A party of four travelers comes to a rickety bridge at night. The bridge can hold the weight of at most two of the travelers at a time, and it cannot be crossed without using a flashlight. The travelers have one flashlight among them. Each traveler walks at a different speed: The first can cross the bridge in 1 minute, the second in 2 minutes, the third in 5 minutes, and the fourth takes 10 minutes to cross the bridge. If two travelers cross together, they walk at the speed of the slower traveler. What is the least amount of time in which all the travelers can cross from one side of the bridge to the other?
Obvious (but incorrect)
Not so obvious (correct answer)
Next…
A party of four travelers comes to a rickety bridge at night. The bridge can hold the weight of at most two of the travelers at a time, and it cannot be crossed without using a flashlight. The travelers have one flashlight among them. Each traveler walks at a different speed: The first can cross the bridge in 1 minute, the second in 2 minutes, the third in 5 minutes, and the fourth takes 10 minutes to cross the bridge. If two travelers cross together, they walk at the speed of the slower traveler.
What is the least amount of time in which all the travelers can cross from one side of the bridge to the other?
Hurray ! I received my bike license today in Japan ! In my first attempt itself ! Wish me congratulations !
Ok some background – I had a bike license from my country. I had not converted it to International License, so I had to opt only for conversion into Japanese license – 外国免許切り替え. I do have a car license obtained in Japan, so I was exempted from written test (which by the way is too difficult, trust me). Instead, I simply had to appear an actual driving test (which is again difficult, mind you). I was required to produce a translated copy of my original license from my country, which can be done in 30 mins in JAF offices. Rest are your passport (both old, new ones), an application form, few stamps which you can buy right there. I live in Tsukuba, so I had to goto Ibaragi License Center, which is situated in Mito. You can go during weekdays, from 09:00AM. Believe me, you better arrive early. There is plenty of crowd, for all sorts of licenses, and usually you have to spend almost whole day there, in case you pass the test and obtain the license. I spent close fo 6 hours today, but in the end I was a happy guy.
Once they receive your application, they ask you few questions about your original license – How did you obtain it? Where did you practice? What kind of test did you appear for this license? How many cc bike? Learner’s license? etc. Unless there are any problems, you are then handed over a course map, and timings when your test will start. Mind you, the course map is given so that you “memorize” it, period. You are required to drive the exact course map, observing lane rules, traffic signs, signal, right or left indication, speed or slow. The usual advice is that you have some 45 mins in hand before your test starts. Thus, you should actually take the course map in hand, and walk the entire course by foot atleast once. I did that, making a mental note of the lanes, the distance around which I should turn on the indicator, and so on – till I burnt the course map into my brain cells !
The most difficult are – Ipponbashi, and Slalom – for me. And I did literally walk on foot imagining I was on bike before my test started.
I believe I was the only one appearing a bike test today – why I was alone sitting in the waiting room till my turn came. The instructor was kind enough to walk me (verbally) through the course map once again. He also gave me a brief about the bike – Honda CB750 – and for my own safety, in case I should fall, had me wear elbow, knee, body protector before the test.
When the actual test started, the instructor actually sits in a watchtower, from which he had a complete view of the course. Once he gave a go, I just drove the same path I had walked on foot earlier. I was particularly careful about slow down sign, lane change, and making sure that I move my head from left to right wide enough to show that I am taking visual confirmations before proceeding. At one point when making a turn I did step down, but I guess that was OK, because if I’d had done the same on Ipponbashi or Slalom, or even S-letter, or Crank, I am out without any further discussion. Phew, I did feel once the bike will stop during Crank, but I was lucky enough to make it till the final stop.
In the end, the instructor appraised my driving skills, and gave me the golden word – “合格” !