Wednesday, November 30, 2011

C Output Type Question


main( )
{
static int a[ ] = {0,1,2,3,4};
int *p[ ] = {a,a+1,a+2,a+3,a+4};
int **ptr = p;
ptr++;
printf(“\n %d  %d  %d”, ptr-p, *ptr-a, **ptr);
*ptr++;
printf(“\n %d  %d  %d”, ptr-p, *ptr-a, **ptr);
*++ptr;
printf(“\n %d  %d  %d”, ptr-p, *ptr-a, **ptr);
++*ptr;
printf(“\n %d  %d  %d”, ptr-p, *ptr-a, **ptr);
}

Try to find the output !!
This question is seemingly quite unconventional, but try it out without using a compiler, and then compare your results with:


1 1 1
2 2 2
3 3 3
3 4 4

Tuesday, November 29, 2011

Brain-Teaser


Try this without googling the answer.. also please try to justify your assumptions if any ?

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?


Sunday, November 27, 2011

Acyclic Linked-List Detection Algorithm

Write a function that takes a pointer to the head of a list and determines whether the list is cyclic or acyclic. Your function should return false if the list is acyclic and true if it is cyclic. You may not modify the list in any way.

int determineTermination( node *head )
        { 
           node *fast, *slow;
           fast = slow = head; 
                  while( true )
                       {
                           if( !fast || !fast->next ) 
                              return false; 
                           else if( fast == slow || fast->next == slow ) 
                              return true; 
                           else 
                              {
                                 slow = slow->next; 
                                 fast = fast->next->next; 
                              }
                        }
        }


Leave your comments below the post.
Happy coding :)

Microsoft Moderate Programming Question

An important interview as well as optimised solution for finding the largest sub array sum in an array which may contain 0, positive and negative integers.


#include<stdio.h>
main()
{
int sumtillnow=0,sum=0,i=0;
int a[7]={-1,11,13,12,-90,23,5};
for(i=0;i<7;i++)
{
sumtillnow+=a[i];
if(sumtillnow>sum)
 sum=sumtillnow;
if(sumtillnow<0)
 sumtillnow=0;
 }
printf("%d",sum);
}

Saturday, November 26, 2011

One line of code for finding out whether a number is power of 2 or not

(An obnoxious looking piece of code) 

If the following LOC returns true, find out what is x here ?


if((x&&!(x&(x-1))==1)) 


Study the Bit-wise operators well, many problems and contests have questions which become quite easy if these are used !!
 The above code is a simple one line test for testing if a variable entered or passed is a power of 2.

Amazon easy simple output question


Another of internet giant Amazon's technical written test question. This question tries to make filter out candidates who have come to attend the examination after reading refreshers of C language. Delve into your K&R's to figure it out.

main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}

Hints:
\n - newline
\b - backspace
\r - linefeed
.
.
.
.
Solution coming soon !!

Strange are the ways of C


Have look at the following snippet, you can see 4 values that which will be printed for each iteration of the loop when you run it. Actually they all are just different ways of representing the same idea !! Try it out on www.codepad.org to see and believe !!

main()
{
char s[ ]="man";
int i;
for(i=0;s[i];i++)
printf("\n %c %c %c %c ", s[ i ] , *(s+i) , *(i+s) , i[s] );
}

So do you also want to become a good programmer someday ??

It is no secret that every programmer cherishes a dream of hitting the headlines of the for doing something really great. Although there are not many world famous programmers in this world, and the best ones rarely opt for a life in public. In most cases it is because of the nature of the work which forces them to hit their machines harder each passing day.
Believe me its no joke and neither it is a cakewalk to be successful as a programmer, because programming in the first place is not about learning programming languages, instead it is about the machine, logic and semantics. The code is just something which comes on in after the three have been developed. Choice of a particular language is purely something that is your own and u needn't be advised by anyone. Lastly, expecting overnight success just because you completed that Complete Reference or Bible of XYZ language is called stupidity and i pray to God that such people get some brains soon enough. Dear Reader, after reading the book you need to implement the constructs and syntax on your machine and some problems. Open up a few websites and blogs (like this one) and start to think about the solutions to them, and if you have done enough then start optimizing them or finding new methods to solve the same problem.
There are a few really great articles i would like you to refer :

1.  How to be a good programmer.
2.  How to be come a good programmer in.....

Print your Name without the semicolon in a C program

Okay.. this question used to be quite a favorite at coding championships 4-5 years back. The question seemed tough as all programming books start with the statement that "All statements in ' XYZ ' language end with a ( ; ) semicolon, except for a few conditional, looping and other control structure constructs. Actually the problem lies in the fact that we try to use standalone printf() statements in our solutions.
Here's a solution for it :

main()
{
 if(printf("MY NAME")) {}
 else {}
}

Easy-3


Work out the solution for this one, and i am sure many of you would have found out a new technique at the end of the solution !!

swap(int *e, int *f);
main()
{
int x=10,y=8;
swap(&x,&y);
printf("x=%d y=%d",x,y);
}
void swap(int *a, int *b)
{
*a ^= *b, *b ^= *a, *a ^= *b;
}

Friday, November 25, 2011

Easy-2

This is second in line with the easy posts.. after a while we shall move onto more tricky ones.. A problem a day keeps the Alzheimer's away.

void main()
{
static int i=i++, j=j++, k=k++;
printf(“i = %d j = %d k = %d”, i, j, k);
}

Guess the output !!

Easy-1



There are numbers from 1 to N in an array, out of these, one of the number gets duplicated and one is missing. 
The task is to write a program to find out the duplicate number.
(example: 1 2 3 3 4 5)
Already Solved the question in your head ?? Take this.. : No using any auxiliary space like arrays.

Welcome welcome..

hey. if you are reading this you are either interested in algorithms, logical problem solving and stuff.. or the second case is that you were just clicked around to get in here. It's alright buddy if you don't want to go on further hit CTRL+W on your keyboard..
OK. so you are still here.. This blog is about all the logic related stuff which includes small logical puzzles to hard artificial intelligence problems.. You can post in your solutions, problems, hints or whatever you think is relevant to the post. I hope we gather in support of a few like-minded and intellectual people pretty soon to help us out.
Frequently i shall post in a new problem and wait you to solve them, if you need help i will drop in hints and who knows you might turn out a better solution !!