Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Tuesday, 26 March 2019

Merits and Demerits of array

Array in C

Merits:

(a) We can easily access each element of the array.

(b) Not the necessity to declare too many variables.

(c) Array elements are stored in a contiguous memory location.

Demerit:

(a) Wastage of memory space. We cannot change the size of the array at the run time. 

(b) It can store only similar type of data.

Wild Pointer

Wild Pointer:


A pointer in c which has 

not been initialized is 

known as a wild pointer.


Example:


C program:


#include<stdio.h>

int main(){

int *ptr;

printf("%u\n",ptr);

printf("%d",*ptr);

return 0;

}


Output: Any address

Garbage value


Here ptr is a wild pointer 

because it has not been 

initialized. 

Dangling Pointer Solution

The solution to this problem: 
Make the variable x is a static variable. 

C program:




















Output: 26

Dangling pointer:

Dangling pointer:

If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while the pointer is still pointing such memory location. Such pointer is known as Dangling pointer and this problem is known as dangling pointer problem.
          
Initially:        




Later: After Removing a





 C program:





















Output:

Initially:
Output: Garbage Value.


After Remove line 7
Output: 26


                                   


Monday, 25 March 2019

The Interview Question Today#26(25-3-19) (Swap two variables without using third variable.)

Question 

Swap two variables without using third variable.

Example:

Input: A=5, B=10
Output: A=10 B=5

Solution:

C program:

#include<stdio.h>
int main(){
int a=5,b=10;
    printf("\nBefore Swap: a= %d  b=  %d",a,b);
    a=a^b;
    b=a^b;
    a=b^a;
    printf("\nAfter Swap: a= %d  b=  %d",a,b);
}

Output:

Before Swap: a= 5  b=  10

After Swap: a= 10  b=  5

Aptitude Hack#30(25-3-19)

Question:

The captain of a cricket team of 11 members is 26 years old and the wicketkeeper is 3 years older. If the ages of these two are excluded, The average age of the remaining players is one year less than the average age of the whole team. What is the average age of the team?

A) 23 years
B) 24 years
C) 25 years
D) None of these


Sunday, 24 March 2019

The Interview Question Today#21(16-3-19)

Question:

Comparison Counting Sort



Solution:

 Approach:

1. First of all, I am reading n elements in the array a[]. While reading the array elements I have also calculated the maximum element.

2. Now the actual sorting is done in the counting_sort() function. Here I am counting the occurrence of each element in the array a[] and then storing it at the index equal to the value of that element. For example, the occurrence of element 5 will be stored at index 5, the occurrence of element 9 will be stored at index 9 and so on.

3. As the value at each index in the count[] array is the occurrence of that index or element, so the elements are printed in ascending order by printing each index number of times equal to its corresponding value.


 ALGORITHM ComparisonCountingSort(A[0..n - 1])

//Sorts an array by comparison counting

//Input: ArrayA[0..n - 1] of orderable values

//Output: ArrayS[0..n - 1] of A’s elements sorted in nondecreasing order

for i ← 0 to n - 1 do


Count[i] ← 0

for i ← 0 to n - 2 do

forj ← i + 1 to n - 1 do

ifA[i] < A[j]

Count[j] ← Count[j] + 1

else Count[i] ← Count[i] + 1

for i ← 0 to n - 1 do

S[Count[i]] ← A[i]

return S


C Program:


/*  C Program for counting sort */

#include <stdio.h> /*  Counting sort function  */

void counting_sort(int A[], int k, int n)

{

    int i, j;

    int B[15], C[100];

    for (i = 0; i <= k; i++)

        C[i] = 0;

    for (j = 1; j <= n; j++)

        C[A[j]] = C[A[j]] + 1;

    for (i = 1; i <= k; i++)

        C[i] = C[i] + C[i-1];

    for (j = n; j >= 1; j--)

    {

        B[C[A[j]]] = A[j];

        C[A[j]] = C[A[j]] - 1;

    }

    printf("The Sorted array is : ");

    for (i = 1; i <= n; i++)

        printf("%d ", B[i]);

}

/*  End of counting_sort()  */ /*  The main() begins  */

int main()

{

    int n, k = 0, A[15], i;

    printf("Enter the number of input : ");

    scanf("%d", &n);

    printf("\nEnter the elements to be sorted :\n");

    for (i = 1; i <= n; i++)

    {

        scanf("%d", &A[i]);

        if(A[i]>100){//max element should 100

            printf("error max element should <=100");

            exit(0);

        }

        if (A[i] > k) {

            k = A[i];

        }

    }

    counting_sort(A, k, n);

    printf("\n");

    return 0;

}


Output:


Enter the number of input : 5 Enter the elements to be sorted :

3

2

7

9

1

The Sorted array is : 1 2 3 7 9

The Interview Question Today#18(13-3-19)(Elements in a given array are distinct)

Question:

Determines whether all the elements in a given 

array are distinct


Example:


Input: arr[]={1,2,3,4,5,3}
Output: Repeated Element Exists




Solution:

Approach:

ALGORITHM UniqueElements(A[0..n - 1])

//Determines whether all the elements in a given array are distinct

//Input: An array A[0..n - 1]

//Output: Returns “true” if all the elements in A are distinct

// and “false” otherwise

for i ← 0 to n - 2 do

for j ← i + 1 to n - 1 do


if A[i] = A[j] return false

return true


C program:


#include <stdio.h>

int main(){

 int array[100],d, c, n,flag=0;


  printf("Enter number of elements in array\n");

  scanf("%d", &n);

  printf("Enter %d integer(s)\n", n);

  for (c = 0; c < n; c++)

    scanf("%d", &array[c]);

   for (c = 0; c < n; c++){

    for(d=c+1;d<n;d++)  {

    if (array[c] ==array[d])    /* If required element is found */    {

        flag=1;

      printf("%d is present at location %d and %d.\n",array[d], c+1,d+1);

      break;    }

  }

  }

  if (flag==0){    printf("No repeated element present in the array.\n");

  }

  if (flag==1){

    printf("Repeated element present in the array.\n");

  }

  return 0;

}

Output:

Enter number of elements in array

5

Enter 5 integer(s)

4

5

6

4

5

4 is present at location 1 and 4.

5 is present at location 2 and 5.

Repeated element present in the array.

The Interview Question Today#25(20-3-19)(Rectangles overlap or not.)

Question:
Given two rectangles, find if the given two rectangles overlap or not.

Example:














Solution:

Approach:

Two rectangles do not overlap if one of the following conditions is true.
1) One Rectangle is inside other rectangle.

2) One rectangle is above the top edge of other rectangle.

3) One rectangle is on the left side of the left edge of other rectangle.



C program:

#include<stdio.h>

struct Point
{
int x, y;
};

int doOverlap(struct Point l1, struct Point r1,struct Point l2,struct Point r2)
{


    if(l1.x<=l2.x && l1.y>=l2.y && r1.x>=r2.x && r1.y>=r2.y)
        return 1;

    if(l1.x>=l2.x && l1.y<=l2.y && r1.x<=r2.x && r1.y<=r2.y)
        return 1;
// If one rectangle is not on left side of other

if (l1.x > r2.x || l2.x > r1.x)
return 0;


// If one rectangle is not above other
if (l1.y < r2.y || l2.y < r1.y)
return 0;

return 1;
}
int main()
{
struct Point l1,b1;
struct Point l2 ,b2;
printf("Enter the Coordinates of rectangle 1 length");
scanf("%d %d",&l1.x,&l1.y);
printf("Enter the Coordinates of rectangle 1 breath");
scanf("%d %d",&b1.x,&b1.y);
printf("Enter the Coordinates of rectangle 2 length");
scanf("%d %d",&l2.x,&l2.y);
printf("Enter the Coordinates of rectangle 2 breath");
scanf("%d %d",&b2.x,&b2.y);

if (doOverlap(l1, b1, l2, b2))
printf("Rectangles Overlap");
else
printf("Rectangles Don't Overlap");
return 0;
}


Output:



Enter the Coordinates of rectangle 1 length0

10

Enter the Coordinates of rectangle 1 breath10

25
Enter the Coordinates of rectangle 2 length11
20
Enter the Coordinates of rectangle 2 breath20
25
Rectangles Don't Overlap