Showing posts with label Interview Question and Answers. Show all posts
Showing posts with label Interview Question and Answers. Show all posts

Sunday, January 15, 2012

Little endian to big endian

//2-byte number
int SHORT_little_endian_to_big_endian( int i)
{
    return (( i>>8)&0xff)+((i<<8)&0xff00);
}
//4-byte number
int INT_little_endian_To_big_endian(int i)
{
    return((i&0xff)<<24)+((i&0xff00)<<8)+((i&0xff0000)>>8)+((i>>24)&0xff);
}

Sunday, March 19, 2006

Give Me a String at Random from THIS File

Constraint:
1) You can make only one sequential pass through the file.
2) You may not store any additional information like a table of offsets.

I would like to receive the answers from you all. After receiving answers from you i will post the answer.

PS: Seems this question also asked in Microsoft Inverview.

--------------------------------------------------------------------

Solution: ( Taken from a book, if anyone know best solution, please post comment )

The basic technique is to pick survivors, and recalculate as you go along. This is so com-putationally inefficient that it's easy to overlook. You open the file and save the first string. At this point you have one candidate string with a 100% probability of picking it. While remembering the first string, you read the next string. You now have two candidates, each with a 50% probability. Pick one of them,save it, and discard the other. Read the next string, and choose between that and the saved string,weighting your choice 33% toward the new string and 67% toward the survivor (which represents the winner of the previous two-way selection). Save the new survivor.

Keep on doing this throughout the entire file, at each step reading string N, and choosing between that(with probability 1/N) and the previous survivor, with probability (N - 1)/N. When you reach the endof the file, the current survivor is the string picked at random!

Thursday, December 08, 2005

sizeof() Vs strlen()

In C language Difference between sizeof() and strlen()

Consider the following example:

/* Example for sizeof( ) , strlen( ) */
#include 
main()
{
       char String[]="Hello";

       printf("\n SIZE OF String %d STRING LENGTH %d", 
       sizeof( String ), strlen( String ) );
}

Result:
SIZE OF String 6 STRING LENGTH 5.

Every String contains a NULL character( '\0' ) at the end. The sizeof() function will include that NULL character also for calculating string size but strlen() function not.

Why is sizeof('a') not 1?
Perhaps surprisingly, character constants in C are of type int, so sizeof('a') is sizeof(int) (though it's different in C++).

Result:
In Turbo C output is: 2
In Turbo C++ output is: 1

Thursday, December 01, 2005

Is It possible to call a Static function by a Function Pointer?

Yes It is Possible. Eventhough, static functions are local to that file, if we assign that function address to a same type (prototype should be strictly same, otherwise result unknown) function pointer. We can call that function indirectly...