Thursday, April 25, 2013

Puzzles - Part2

Few more brain teasers if you really think they can tease you
Disclaimer: None of the puzzles are mine. I am just writing them down so people can read it.
Puzzle:
you are playing a coins game with your friend. there are total of 100 coins on the table. At any playing turn, the player has to pick minimum 1 coin and can pick maximum of 8 coins. last one to pick the coins from the table wins. if you are given a chance to pick first can you win? 

Now when we look at the puzzle above the total number of coins are 100 and I can pick up anything in the range of 1 to 8. Does that guarantee me the win? We being humans normally think on the forward way. Here is the way when we have a goal in mind can we think in reverse direction and come with a solution.

there are only three rules in the game above.
1) last one picking up the coins win
2) you have to pick the coins all the time
3) you have to restrict your opponent from winning

if you look at the the third rule, that's the whole point of this game. Given a goal how can you achieve it?
so lets  think in a reverse manner.
To win I will need to pick up the last coins from the table. I can pick maximum of 8 and minimum of 1 coin.
so that means as long as I leave the second last turn on 91, I will win.
Ok how do I make sure that I will have someone reach 91. Now instead of having 100 coins lets say we just have 91 coins in total. In that case the second last turn becomes 82.
so if we put down the numbers in this series they look something like this
91 82 73 64 55 .....10 1
now if want to put a formula around it
(max + min)X playing turn + 1 will guarantee me the win
so now the first aim is not 100 .. first aim is make sure that when the next player plays  you need to keep these many coins of the table
so now can solve the puzzle above

==================================================================

Puzzle:
This is normal puzzle known to all. .
There are three lions and three humans on the bank of a river. You have to take them across the river on the other bank. You are the only allowed person who can row the boat across the river. At any time you can let the number of lions exceed the number of humans else they will eat the human. You will need to carry at least one passenger in any direction you travel. How will you transport them? 

Lets represent Lions as L,  Humans as H and then do the transportation
HHH LLL

HHHLLL
               -----HL----->
                                     HL
               <-----H-----
HHLL                            L
              -----HL----->
HHL                              HLL
              <----L-------
HHLL                           HL
               ----HL----->
HL                                 HHLL
               <----L-------
HLL                               HHL
                ----HL------>
L                                     HHHLL
                <----H------
HL                                  HHLL
                 -----HL---->
                                        HHHLLL

==================================================================


Puzzle:
Find the minimum number of cuts you will need to make in  cheese cube to generate 27 cubes? 

try two cuts across each side of the cube which is not cut. So on the top, on the side and horizontally.
minimum of 6 cuts. Mathematically if you try you can not do in 2^4=16, ie in 4 attempts. So you may suggest 5 but that would be wrong)


===================================================================

Puzzle:
Using a 4 minute hourglass and 7 minute hourglass measure 9 minutes




step4 minute7 minuteTIME
1FLIPFLIP0
2FLIP3 minutes remaining4 minutes
31 minute remainingFLIP7 minutes
40FLIP8 minutes
5009 minutes

Friday, April 12, 2013

Are you smart enough to work at Google - Part 1

These are few of the puzzles from the book.
I try to solve few and give few answers as I found them on several places and the book itself .
All the points I make here are mine (I understand that I can be wrong and I will take any corrections)
These puzzles are from book written by Mr. William Poundstone. You can visit his website at home.williampoundstone.net  . I will strongly recommend his book for anyone who likes to read about how different people view different things via puzzles

DISCLAIMER: I have not invented the puzzles nor the solutions. I am just trying to explain my understanding of the problems for in general audience like me

1) The penny size problem : This is directly from the book, No contribution from me. All contributions to William PoundStone

Here is the problem
You are shrunk to the size of a penny. Your mass is reduced but your density remains same. You are thrown in a grinder and the blades start in 60 seconds, what would you do?

As been pointed out always, a good interviewer looks at your approach and others looks at your final answer.
So normal answers would be stick to the wall, get below the blades. stick with the blades, make rope of my shoe laces and climb up etc etc
but in general we do see this problem when we see ants carrying the food. So when a small ant carry the food which ways x number of times greater than its own weight. How can it do that? We never wonder .. the problem was answered in a physics class somewhere in school when at the least I (not sure about) was busy poking friends at the back benches.
so basically if the ant can carry that higher weight than itself , if the shrunk person can jump from the jar and fall of, will he be able to survive. By adding the gravity, fall of speed, height of jar and all other variables, the question comes will you be able to first make a jump of that height and if you can when you land on other side will you be to survive?
The answer to this question is not about  how can you get out of the jar but more of can you think about the scale issue. In the today's world most buzzing word around technology is scalability. This problem tells at different scales you do get different benefits with compromising few on different scale.
================================================================
Here is another puzzle
What comes next in series ? SSS --- SCC -- C -- SC ?

When I started looking at this problem, as a grown up person the first thing I started to look at can I find any relation between S and C and their occurrence. *SLAP* to me .. I couldn't. One thing we always lose being grown up is the basic thing.
Second thing lets calculate -- yeah I have studied mathematics I can solve it by maths -- *SLAP* to me
after breaking my head for more than  60 minutes I did gave up. I still claim I did not know the answer and I still dont claim I found the solution. What I got was luck some othertime
On a paper I was drawing face pictures and then I was explaining it to the friend how to make a friend

You first draw two Straight vertical lines and then draw two Curves to connect those lines. Now you draw one small size Straight line  and one medium size horizontal Straight line right in the middle of the shape you just draw. Then you draw two inverted Curves one each around the vertical line in the middle. now draw a Circle below the curves and you have a face. and then the lightening struck

SSS - three straight lines
SCC- one line two curves
C - just a curve
SC- line and curve

now as an engineer first thought was trigonometry. Drew all shapes and then left the problem there.
Then you get this answer when you think when start making just the third place single C. can these be just the english alphabets ?
lets try
SSS - three lines - A
SCC - straight line and two curves or circles B
C - just a C (clue)
SC- becomes D
so next is E and it will be SSSS

================================================================
A similar puzzle to above one is like below 
1111 = 0
1896 = 4
3564 = 1
8888 = 8
9999 = 4
2222 = 0
5369 = ?

If you looked at the shapes in previous problem there is another way to look at the numbers now
straight lines and circles makes the numbers.
so each line is counting the number of circles in that number and that's one way we look at the problem.
We always forget our natural instinct when someone asks us for puzzle. we get into maths rationalism etc etc. But when we see any monument most of the time we notice the shape.
So I did learn my lesson for not forgetting the shape factor in life. :)
===============================================================
There is one puzzle based on general physics
When there's wind blowing , does the round trip by plane change? Is yes then will it reduce or increase?

Answer to this is Yes the time will increase.
you ask Why?
first of lets forget plane and put a cycle on the road and the wind behind you. It ofcourse increases your speed. Wow now you must be thinking that this idiot just says it takes longer time. Now when you are coming back and the fighting against the wind it will take you forever to reach back.
To simplify more lets look at the boats travelling in sea. When they are going with the tide they are fast but when the tides are against them it just keeps on adding time. Now you may say that plane engines powers etc etc. You can try it mathematically with putting few numbers around the equation and you will find a  suitable answer yourself as well :)

================================================================

One puzzle which I loved the most from William's book "Are You Smart Enough to Work at Google?" 
The below puzzle basically tells how an innovative mind should think and how a hardworking mind can think

Find the next number in the series 
10, 9 , 60, 90, 70, 66 , ? 

As dumb as I am, this time I was determined to solve this problem without reading the solution in the book.
So routine try to fit the series in some maths equation - fail
try to find relation between numbers - fail
try to some numbers logic around it - fail

After 4 hours i kept the list vertically and I could reach only that far and slept off. Next day while on subway metro I had the paper in hand I was looking at the numbers and was getting bored so I just started writing the numbers again and it did not strike me.
It was more of an itchy feeling that I wanted to get the solution and find the solution. Why not try internet? Yeah when you are not giving interview you can do that but that does not help me cause I will run across the solution everybody has written and i will just read it. And that's what happened after giving it a lot of try.
An INNOVATIVE mind thinks and a hard-working mind sometimes need the help of innovative mind.

10  - ten (3)
9    - nine (4)
60  - sixty (5)
90  - ninety (6)
70  - seventy (7)
66  - sixty-six (8)

Now if you look at it, it looks a lot simpler, you just need to find a number having 9 letters. The catch here is
if you notice it
there are more three letter numbers like one two but ten was chosen
there are more four letter numbers four five but nine was chosen
so it is the maximum number having those many letters when it spelled out.

so now a 9 letter maximum number : I could only get 96.
But if you are being interviews at google, this is just an acceptable answer. Because if you look at why they chose google as company name and a bit of history you will read a number

"one googol" - count the letters in it including the space bar. thats the correct answer? think about can you make it "ten googol"

you may counter argument that why not use million as 7 letter word. That is because in this question if notice all these numbers are represented as cardinal number. So if you type 100 .. it becomes "one hundred"

I will try to find more puzzle in coming days to have some more food for brains :) 

Wednesday, April 10, 2013

setting up passwordless ssh

Why do you need password-less ssh ?
You want to use Linux and OpenSSH to automize your tasks. Therefore you need an automatic login from host A / user a to Host B / user b. You don't want to enter any passwords, because you want to call ssh from a within a shell script

Is it Secure?
Yes. As long as you do not share your keys files

What are keys?
Keys are encrypted authentication files which automatically get checked when you login from one machine to another.
There are two major key types

Which one to choose? 
Choice is yours. 
DSA keys provide smaller keys and faster operations so I choose dsa most of the times. Please note this may not work with older OPENSSH versions


What do I need ?
at least one linux machine
OPENSSH installed
one user with ssh permissions

How do I Do it?

  1. if want then create a separate user . I will create user named test
    • useradd test
    • login to users shell : sudo su - test
  2. create .ssh directory default location. You can store the keys anywhere you need but then you will need lot of other configurations. So lets go with default
    • mkdir -p ~/.ssh
  3. create the key 
    • command which creates keys is ssh-keygen. First decide which type and then run
    • ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa
  4. Above command will create two files in directory ~/.ssh. One is your private key file and other is public key file. change permission of both files to read only by user
    1. chmod 400 ~/.ssh/id_dsa*
  5. copy the contents of the .pub file into another machine or same machine's .ssh/authorized_keys
  6. Change permission for RW user only. chmod +600 .ssh/authorized_keys
  7. now you can ssh from first machine to second machine without inputing your password