Saturday, September 3, 2011

Eat Drink Code !!!




Snapper chain (Time limit : 45 min)
The Snapper is a clever little device that, on one side, plugs into an output socket, and, on the other side, exposes an output socket for plugging in a light or other device.

When a Snapper is the ON state and is receiving power from its input plug, then the device connected to its output socket is receiving power as well. When you Snap your finger – making a clicking sound – any Snapper receiving power at the time of the snap toggles between the ON and OFF states.

In hopes of destroying the universe by means of a singularity, I have purchased N Snapper devices and chained them together by plugging the first one into power socket, the second one into the first one, and so on. The light is plugged into the Nth snapper.

Initially, all the Snappers are in the OFF state, so only the first one is receiving power from the socket and the light is off. I snap my finger once, which toggles the first snapper into ON state and gives power at the second one, I snap my fingers again, which toggles both Snappers and promptly cut power off from the second one, leaving it in the ON state, but with no power. I snap my fingers the third time, which toggles the first Snapper again and gives power to second one. Now both Snappers are in the ON state, and if my light is plugged into the second Snapper it will be on.

I keep doing this for hours. Will the light be on or off after I have snapped my fingers K times? The light is on if and only if it’s receiving power from the Snapper it’s plugged into.

Input
The first line of the input gives the number of test cases, T. T lines follow. Each one containing two integers, N  and K.

Output
For each test case, output one line containing “Case #x:y”,where x is the case number starting from 1 and y is either ON or OFF, indicating state of light bulb.

Limits 1<=T<=10000 ,1<=N<=30,0<=K<=10^8

Sample:
Input
4
1 0
1 1
4 0
4 47
Output
Case#1:OFF
Case#2: ON
Case#3:OFF
Case#4:ON

For my Solution see the comment....

5 comments:

  1. After 25 min of logic building and 10 min of writing...my solution :)

    #include
    int main(void)
    {
    int T,c=0,N,K,Y,Z;
    scanf("%d",&T);
    while(T--)
    {
    scanf("%d %d",&N,&K);
    if(K<=N) {printf("Case #%d:OFF\n",c++);continue;}
    Y=1<<N;
    Z=Y-1;
    if ((K-Z)%Y) printf("Case #%d:OFF\n",c++);
    else printf("Case #%d:ON\n",c++);
    }
    return 0;
    }

    ReplyDelete
  2. your code isn't working for the second test case

    #include
    #include

    using namespace std;

    int main(){
    int T, n, k;
    cin >> T;
    for (int i = 0; i < T; i++){
    cin >> n >> k;
    cout << "Case #" << i + 1 << ": ";
    cout<<((k&(1<<n)==(1<<n))?"ON":"OFF")<<"\n";
    }
    _getch();
    return 0;
    }

    ReplyDelete
  3. Yup! Thank God this was not caught by interviewer :)
    Or May be he neglected it!
    Anyways, Cheers !

    ReplyDelete
  4. Hi Yash ,
    Could you please tell me little bit about your technical interview(s) of Belzabar .
    Thanks in advance !

    ReplyDelete
    Replies
    1. Hi Afroz,
      There were five rounds:
      1) Technical MCQ round, cutoff ~ 50%
      18 were shortlisted out of 120+ students.
      2) Followed by this coding round, where 6 were shortlisted.
      3) Technical Interview , simple questions based on linked list, trees, and some questions based on resume.
      4) HR Interview, tell us about your self,where do you see yourself in 5 years etc.... Just be honest and you will clear this round :)
      2 survived after these interviews.
      5) Final Interview by Director.
      Very simple technical questions... swap space, simple c concepts, Linux commands etc..
      Both of us cleared this round!



      I Hope this helps!

      Cheers!

      Delete