Categories
Programming

Switch two integer values without using additional memory

Cool trick. Useful to know beyond just interviews.
Register_1 = Register_1 + Register_2;
Register_2 = Register_2 –  Register_1;
Register_1 = Register_1 – Register_2;
Starting
1) R1=A
2) R2=B
R1 = R1 + R2
1) A+ B
2) B
R2 = R2 – R1
1)A +B
2) A+ B – B = A
R1 = R1 – R2
1) A+B -A = B
2) A
Done
1)R1=B
2)R2=A
Categories
Uncategorized

Subtract all the characters in S1 from a string S2

Given two strings S1 and S2. Delete from S2 all those characters which occur in S1 also and finally create a clean S2 with the relevant characters deleted.

 

(Another problem from http://www1.cs.columbia.edu/~kns10/interview/)

Categories
Uncategorized

Write a simple lexical analyzer

Write a small lexical analyzer – interviewer gave tokens. expressions like “a*b” etc.

( Another from: http://www1.cs.columbia.edu/~kns10/interview/ )

Categories
Uncategorized

Spiral print a 2D array

Write a routine that prints out a 2-D array in spiral order! (another one from http://www1.cs.columbia.edu/~kns10/interview/)

Categories
Uncategorized

Implement uniqe for a sorted array

Write code to extract unique elements from a sorted array. e.g. (1,1,3,3,3,5,5,5,8,8,8,8) -> (1,3,5,9)

Slow ball question but good for practice. (I got it from http://www1.cs.columbia.edu/~kns10/interview/)

Categories
Uncategorized

Count the bits in a 32 bit integer

Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution). (Got this question from http://www1.cs.columbia.edu/~kns10/interview/ )

Categories
Programming

Algorithm to find the center of a list in O(n) time

Same technique can be used to find loops in a liked list. If you advance through the list one pointer at rate X, and another pointer through the list at rate (1/2)X then when the faster pointer is at the end of the list the slower pointer will be pointing to the center element of the list.

<pre class=code>

// findCenterElement – finds the element at the center of the list

node *findCenter(node *curList){

node *centerNode = curList;

node *curNode = curList;

if(curList == (node *)NULL)

return((node *)NULL);

if(curNode->next != (node *)NULL){

while(((curNode->next)->next) != (node *)NULL)

{

curNode = (curNode->next)->next;

centerNode = (centerNode->next);

}

}else{

return(curNode);

}

return(centerNode);

}

</pre>

Categories
Programming

Shuffle a deck of cards

The way I found the problem on the web:

Give me an algorithm to shuffle a deck of cards, given that the cards are stored in an array of ints.

Categories
Mixing Problems

Mixing red and blue paint – show the final ratio after a single mixing.

If you have two buckets, one with red paint and the other with blue paint, and you take one cup from the blue bucket and poor it into the red bucket. Then you take one cup from the red bucket and poor it into the blue bucket. Which bucket has the highest ratio between red and blue? Prove it mathematically.

Kind of a trick question. What if the buckets only contained 1.1 cups of paint?

Since the paint is conserved we can say that if after the pouring back and fourth over N cycles (here N=1) an ammount a of paint is transfered from the red to the blue bucket then we can see that the relative ratios are:

[(X-A) Red / A Blue] in the red bucket and [A/(X-A)] in the blue bucket.

As logn as (X-A) is greater than A we have proven the above statement.

Categories
Puzzles

Closed room/box with three light bulbs and three switches

There is a room with a door (closed) and three light bulbs. Outside the room there are three switches, connected to the bulbs. You may manipulate the switches as you wish, but once you open the door you can’t change them. Identify each switch with its bulb.

Assumptions:

  1. Assuming the lights are not the newer LED bulbs that dont get warm to the touch we can assume if we leave thei light on it will get warm.
  2. We are powering up the box / it does not exsist before time T0. If not it is possible that the heating and cooling times will make the following simplification not work.

Two bulb Solution

{S1 S2} = {a,b} –> Starting test state – wait for an hour.

{S1 !S2} = {a,!b} –> Toggle switch states and open the door.

  1. Light that was constantly on should now be warm and on.
  2. Light that was constantly off should still be off and cold.
  3. Light that was on and is now off should be off and cold.
  4. Light that was off and is now on shoudl be on and cold.

Tests 1 and 2 uniquely identify S1, and you get S2 by process of elimination.

Three bulb Solution

  1. {S1  S2  S3} = {a,  b,   c} –> Starting test state – wait for X mins.
  2. {S1  S2 !S3} = {a,  b, !c} –> Toggle switch and wait for Y mins.
  3. {S1 !S2  S3} = {a,!b, c} –> Toggle switch and pen the door.

Decoding then is

  1. S1. Light that was constantly on should either be ambient temperature and off or very hot and on.
  2. S2. Should either be heated for X+Y mins and off, or ambient and on.
  3. S3. Should either be heated for X-Y mins and on, or heated Y mins and off.

Four temperatures possible: Ambient, Y, X-Y, X+Y

  1. Ambient and off light is S1.
  2. Ambient and on light is S2.
  3. Heated (X+Y) and on is S1.
  4. heated (X+Y) and off it is S2.
  5. Heated X-Y and on is S3 and heated Y and off is S3. So the medium temperature (either Y or X-Y)  is S3.

From 5 above we can collapse the temperature range to Ambient , {Y, X-Y}, X+Y. So just picking a X/Y ratio that allows for adaquade difference in heating and cooling times is all that is needed.