Thursday, 6 June 2019
Wednesday, 5 June 2019
Aptitude Hack#102(5-6-19)
Question:
Ram and Hari started from A and B, towards B and A at 6.00 am and 7.00 am respectively. They meet each other at 9.00 am and continued towards their respective destinations. Ram reaching B turns back and catches up with Hari before Hari reaches A at 11.00 am. At what time will Hari reach A.
(a) 7.00 pm
(b) 4.00 pm
(c) 5.00 pm
(d) 6.00 pm
Ram and Hari started from A and B, towards B and A at 6.00 am and 7.00 am respectively. They meet each other at 9.00 am and continued towards their respective destinations. Ram reaching B turns back and catches up with Hari before Hari reaches A at 11.00 am. At what time will Hari reach A.
(a) 7.00 pm
(b) 4.00 pm
(c) 5.00 pm
(d) 6.00 pm
Tuesday, 4 June 2019
Monday, 3 June 2019
Sunday, 2 June 2019
Program for Shell Sort in C
Shell short is an improved and efficient version of the insertion sort.
In this algorithm, we sort the pair of elements that are far apart by gap h.
The process is repeated by reducing h until it becomes 1.
![]() |
| Shell Sort |
Algorithm
Following is the algorithm for shell sort.
Step 1 − Initialize the value of h
Step 2 − Divide the list into smaller
sub-list of equal interval h
Step 3 − Sort these sub-lists using insertion sort
Step 4 − Repeat until the complete list is sorted
Program for Shell Sort in C
#include<stdio.h>
void sort(int a[],int n)
{
int gap,i,j,temp;
for(gap=n/2;gap>0;gap/=2)
{
for(i=gap;i<n;i+=1)
{
temp=a[i];
for(j=i;j>=gap&&a[j-gap]>temp;j-=gap)
a[j]=a[j-gap];
a[j]=temp;
}
}
}
int main()
{
int a[20],i,n;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;++i)
{
scanf("%d",&a[i]);
} sort(a,n);
printf("\nArray after shell sort:\n");
for(i=0;i<n;++i)
printf("%d ",a[i]);
return 0;
}
Output
Enter number of elements:5
Enter array elements:
56 7 2 9 12
Array after shell sort:
2 7 9 12 56
C program to print the truth table for XY+Z
June 02, 2019C interview Question, C program to print the truth table for XY+Z, Tricky Interview QuestionNo comments
C program to print the truth table for XY+Z
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr(); //to clear the screen
printf(“XtYtZtXY+Z”);
for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
for(z=0;z<=1;++z)
{
if(x*y+z==2)
printf(“nn%dt%dt%dt1”,x,y,z);
else
printf(“nn%dt%dt%dt%d”,x,y,z,x*y+z);
}
}
OUTPUT:
Print “javaabhigyan” with empty main() in C
June 02, 2019C interview Question, Print “javaabhigyan” with empty main() in C, Tricky Interview QuestionNo comments
Write a program that prints “javaabhigyan” with empty main() function.You are not allowed to write anything in main().
1.) One way of doing this is to apply GCC constructor attribute to a function so that it executes before main()
#include <stdio.h>
/* Apply the constructor attribute to myStartupFun()
so that it is executed before main() */
void myStartupFun(void) __attribute__((constructor));
/* implementation of myStartupFun */
void myStartupFun(void)
{
printf("javaabhigyan");
}
int main()
{
}
Output:
javaabhigyan
2.) In linux, just override the default definition of _start() function so that it would work as a custom startup code. See this article to understand more.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
}
// _start() function
void _start(void)
{
printf("javaabhigyan");
// Call main() function
int var = main();
exit(var);
}
Now compile this by following command
gcc -nostartfiles -o file file.c
Output:
javaabhigyan
Compute average of two numbers without overflow
June 02, 2019C interview Question, Compute average of two numbers without overflow, Tricky Interview QuestionNo comments
Given two numbers, a and b. Compute the average of the two numbers.
The well know formula (a + b) / 2 may fail at the following case :
If, a = b = (2^31) – 1; i.e. INT_MAX.
Now, (a+b) will cause overflow and hence formula (a + b) / 2 wont work
Improved Formula that does not cause overflow :
Average = (a / 2) + (b / 2) + (((a % 2) + (b % 2)) / 2)
Below is the implementation :
// C code to compute average of two numbers
#include <stdio.h>
#define INT_MAX 2147483647
// Function to compute average of two numbers
int compute_average(int a, int b)
{
return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);
}
int main()
{
// Assigning maximum integer value
int a = INT_MAX, b = INT_MAX;
// Average of two equal numbers is the same number
printf("Actual average : %d\n",INT_MAX);
// Function to get the average of 2 numbers
printf("Computed average : %d",compute_average(a, b));
return 0;
}
Output:
Actual average: 2147483647
Computed average: 2147483647














