Posts

Showing posts from September, 2011

Difference between foo(void) and foo()

Source: http://stackoverflow.com/ Problem: Consider these two function definitions void foo (){ ... } void foo ( void ){ ..... } What is the difference between these two functions? Hint: The answer depends whether this is C code or C++ code.

C code 32 bit vs 64 bit

Source: http://www.gowrikumar.com Problem: The following C program segfaults of IA-64, but works fine on IA-32. int main () { int * p ; p = ( int * ) malloc ( sizeof ( int )); * p = 10 ; return 0 ; }   Update (Oct 30, 2011): Wrong problem. Sorry for the trouble.

C++ Macro Concatenation

Source: http://www.gowrikumar.com Problem: What is the output of the following C++ code? #include <stdio.h> #define f ( a , b ) a ## b #define g ( a ) # a #define h ( a ) g ( a ) int main () { printf ( " %s \n " , h ( f ( 1 , 2 ))); printf ( " %s \n " , g ( f ( 1 , 2 ))); return 0 ; } Update (14 Sept 2011): Solution posted in comments by Prathmesh Prabhu (CSE IITB 2010 Alumnus and Wisonsin Madison II-year Graduate Student)

Arrange in a Sequence

Source: Asked to me by Amol Sahasrabudhe (IITB 2004 Alumnus, Worked at Morgan Stanley Quant Division, Deutsche Bank) Problem: You are given 2n numbers ( 1 to n and 1 to n ). You have to arrange these numbers in a sequence such that between any two i `s , there exists exactly i-1 numbers. Is it possible for all n ? If no, what are the values of n for which this is possible? Disclaimer: I have not been able to solve it. Sudhanshu Tungare (IITB 2008 EE Alumnus, Morgan Stanley) claims to have a solution. Cheers! Update (November 1, 2011): Part solution posted by Nishant Totla (CSE IITB Senior Undergraduate), Richie and Sarat in comments! Complete solution posted by Siddhant Agarwal (EE IITB Alumnus, CMI Grad student) in comments! Thanks a ton.