Thursday, December 27, 2012

Compilation steps of a C program

There are four steps

1. Preprocessing
2. Compilation
3. Assembling
4. Linker

image

STEPS INPUT OUTPUT GCC
Pre Processing source file

preprocessed output (removing all # defines & replacing macros)

gcc -E a.c
output will be printed in console where we can see all the Macro are replaced.

Compilation

Preprocessed output file

Assembly language code

gcc -S a.c
output will be in a.s file

Assembling

Assembly code

Machine code

gcc -c a.c
output will be in a.o

Linker

Machine code

executable file

gcc a.c
output will be a.out file

Monday, December 24, 2012

Cisco Interview Questions

Had 5 rounds of interview

1. Telephonic technical Round
    * steps of compilation
    * storage classes & memory allocation
    * pass by value & pass by reference
    * questions on pointers
    * stack & heap in C
    * Reverse a linked list
    * Middle element of a linked list
    * difference between insertion & quick sort
    * time complexities of all sorting algorithms
    * difference between TCP & UDP

2. Technical round-1 (F2F)
    * TCP/IP layer
    * how ping works
    * IP header
    * questions about data link layer

3. Technical round-2 (F2F)
    * Find a duplicate in an array of 1 to n elements
    * swap without comparison
    * deletion of a linked list
    * find a loop in a linked list
    * set a bit in a integer
    * stack & heap - malloc & calloc
    * some questions in pointers

4. Technical round-3 (F2F)
    * find the sum of array using recursion
    * pattern matching
    * find the intersection of two linked list and questions on projects done.

5. Manager round (F2F)
    * how trace route works
    * how route works
    * ipconfig works
    * some HR questions like , how you manage pressure, strength etc.,

Monday, November 12, 2012

Implement atoi function


#include<stdio.h>
main()
{
// Declarations
    char str[100];
    int i,n,sign;
// Initializations
    i=0;
    n=0;
    scanf("%s",str);
// First string can be '+','-','tab','space','newline',numbers
    if((str[0]!='+') && (str[0]!='-') && (str[0]!='\t') && (str[0]!='\n') && (str[i]==' '))
    {
    if((str[0]<'0') || (str[0]>'9'))
    {
        return ;
    }       
    }
    else
    {
    // Getting the sign value
    sign=(str[0]=='-')?-1:1;
    if((str[i]=='+') || (str[i]=='-') || (str[i]=='\n') || (str[i]=='\t') || (str[i]==' '))
    i++;
   
    // Converting string to integer
    while(str[i]>='0' && str[i]<='9')
    {
        n=n*10+(str[i]-'0');
        i++;
    }
   
    n=n*sign;
    printf("%d",n);
    }           
}

Monday, October 1, 2012

Merging of two sorted arrays

 

Given two sorted arrays A & B where size of A is m & size of B is m+n .  B contains only n number of elements & m empty positions. Sorting should not take any extra memory.

#include<stdio.h>
main()
{
int A[]={1,3,7,10};
int B[7]={2,4,11};
int size_A;
int size_B;
int i;
size_A=sizeof(A)/sizeof(int);
size_B=sizeof(B)/sizeof(int);
size_A--;
size_B--;
int s=size_B;
int B_Elements= (size_B - size_A);
B_Elements--;

while((size_A >=0) && (B_Elements >=0))
{
if( (A[size_A] ) >= (B[B_Elements])){
        B[size_B]=A[size_A];

        printf("\n%d > = %d",A[size_A ] , B[B_Elements ]);
        size_A--;
        size_B--;
}
else
{
printf("\n%d < = %d",A[size_A ] , B[B_Elements ]);
B[size_B] = B[B_Elements];
size_B--;
B_Elements--;
}
}

if ( size_A >=0 ){
    while ( size_A >= 0 ){
B[size_B] = A[size_A];
size_A--;
size_B--;
    }
}
else if ( B_Elements >=0 ) {
    while (B_Elements >=0){
B[size_B] = B[size_B];
    B_Elements--;
    size_B--;
    }
}
else {
printf("Merging done");
}
printf("\n");
for(i=0;i<=s;i++)
printf("%d  ",B[i]);
}

Permutation of a given string


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void swap(char *x , char *y)
{
    char temp;
    temp=*x;
    *x=*y;
    *y=temp;
}
void do_permute(char *a, int start , int size)
{
    int itr;
    if ( start == size ){
        printf("%s\n",a);
    }
    else{
        for(itr=start;itr<size;itr++)
        {
            swap((a+start),(a+itr));
            do_permute(a,start+1,size);
            swap((a+start),(a+itr));
        }
    }
}
main()
{
    printf("PERMUTATION\n");
    char a[100];
    int size;
    scanf("%s",a);
    size=strlen(a);
    do_permute(a,0,size);
}

Sunday, September 30, 2012

Kadane’s Algorithm

 

public class kadane {
    public static void main(String args[])
    {
            System.out.println("Kadane Algorithm");
           
            int A[]={-2, -3, 4, -1, -2, 1, 5, -3};
            int max , max_final , i;
            max=max_final=0;
            int size=A.length;
            for(i=0;i<size;i++)
            {
                max=max+A[i];
                if(max < 0)
                    max=0;
               
                if(max_final<max)
                    max_final=max;
                           
            }
            System.out.println("Maximum sum : "+max_final);
    }
}

Output:

Maximum sum : 7

Some of the interview questions asked recently

 

1. Given a binary tree & a sum value. Find the two nodes in a binary tree whose sum is equal to the given sum.

2. Print all the permutations of a string.

3. Detect and remove the loop in linked list.

4. Given a binary tree , find the maximum rooted subtree (whose sum of its child is greater than other sub trees).

5. Difference between abstract class & packages in Java.

6. Convert Binary Search Tree to Sorted Doubly linked list.

7. Maximum circular subarry sum.

8. Given 3 numbers. Find the lease valid date formed by those numbers.

eg: 1,2 ,3. Output should be : 3/2/1. Note: should take care of cases like valid month & date & leap year.

9. Given binary tree . Print the sum of all the leaf nodes.

10. Given two sorted arrays , ‘A’ of size m and ‘B’ of size n+m. Array B will contain numbers in n positions and ‘m’ empty positions. Merget two Arrays. Note: Should not use extra memory.

Saturday, March 17, 2012

What is the output ?

6. Give sizeof(a) in bytes for the following: (assume 32bit PC)

char *a[10];
char (*a)[10];
char (**a)[20];
char (*a)(int x, char c);

Ans: 40 , 4 , 4 , 4

7. Find out the output :

#include<stdio.h>
main()
{
    int n1 = 10, n2 = 12;
    if( n2=!0)
        printf("Result is:%d\n",(n1/n2));
    else
        printf("cannot divide by zero\n");
}
Ans: Result is:10

8. Find out the output:

#include<stdio.h>
main()
{
    long int one_million;
    one_million=1,000,000;
    printf("One million is %d",one_million);
}
Ans: One million is 1

9.  what is the output ?

#include<stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
main()
{
    printf("%s %s\n",h(f(1,2)),g(f(1,2)));
}
Ans: 12 f(1,2)

10. what is the output?

#include<stdio.h>
main()
{
    static int j=printf("%d",456);;
    printf("%d",j);
}
Ans: Error

11. What is the output ?

#include<stdio.h>
main()
{
int i = 5;
int j;
j=sizeof(i++);
printf("%d",i);
}
Ans: 5

12. What is the output of z?

#include<stdio.h>
main()
{
int z,x=5,y=-10,a=4,b=2;
z=x++ - --y * b / a;
}
Ans: 10

What is the output ?

1. what is the output of the following program ? “alloc failed or alloc successful” ?

#include<stdio.h>
void alloc(int *p)
{
p=(int*)malloc(10*sizeof(*p));

}
main()
{
int *p=NULL;
alloc(p);
if(NULL==p)
printf("alloc failed");
else
printf("alloc successful");
}
Ans: alloc failed

make changes to get “alloc successful” as output.

2. what is the output of the program ?

#include<stdio.h>
main()
{
    struct node
    {
            int a;
            int b;
            int c;
    };
    struct node s={3,5,6};
    struct node *pt=&s;
    printf("%d",*(int*)pt);
    }
Ans: 3

3.  what is the output of the following program ?

#include<stdio.h>
main()
{
    printf(3+"Hello World");
    }
Ans: lo World

4. what is the output of the following program ?

#include<stdio.h>
main()
{
    int a=0;
    if(a=2)
    printf("I am inside IF\n");
    printf("It is correct");
    }
Ans:

I am inside IF
It is correct

5. what is the output ?

#include<stdio.h>
main()
{
    unsigned i=5;
    if(i > -5)
        printf("it is less than -5");
}
Ans: No output