Interview Program Questions
Interview Program Questions
Interview Program Questions
to run when, and how to break them up, called “scheduling”. The Round-Robin policy for scheduling runs
each job for a fixed amount of time before switching to the next job. The waiting time fora job is the total time
that it spends waiting to be run. Each job arrives at particular time for scheduling and certain time to run,
when a new job arrives, It is scheduled after existing jobs already waiting for CPU time
Given list of job submission, calculate the average waiting time for all jobs using Round-Robin policy.
The input to the function waitingTimeRobin consist of two integer arrays containing job arrival and run times,
an integer n representing number of jobs and am integer q representing the fixed amount of time used by
Round-Robin policy. The list of job arrival time and run time sorted in ascending order by arrival time. For
jobs arriving at same time, process them in the order they are found in the arrival array. You can assume that
jobs arrive in such a way that CPU is never idle.
The function should return floating point value for the average waiting time which is calculated using round
robin policy.
Assume 0<=jobs arrival time < 100 and 0<job run time <100.
#include<stdio.h>
int waitingtimerobin(int *job,int *run,int n,int tq)
{
int j,count,time,remain,flag=0;
int wait_time=0,turnaround_time=0,rt[10];
remain=n;
for(count=0;count<n;c++)
{
rt[count]=run[count];
}
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=tq && rt[count]>0)
{
time += rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count] -= time_quantum;
time += time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
wait_time += time-job[count]-run[count];
flag=0;
}
if(count==n-1)
count=0;
else if(job[count+1]<=time)
count++;
else
count=0;
}
printf("waiting time %f",wait_time*1.0/n);
return 0;
}
int main()
{
int ja[],at[];
int n,tq;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d", &ja[i],&at[i]);
}
scanf("%d",&tq);
int *cellsptr=ja;
int *cellsptr1=at;
waitingtimerobin(cellsptr,cellsptr1,n,tq);
return 0;
}
**The LeastRecentlyUsed(LRU) cache algorithm exists the element from the cache(when it's full)
that was leastrecentlyused. After an element is requested from the cache, it should be added to the
cache(if not already there) and considered the mostrecentlyused element in the cache.
Given the maximum size of the cache and a list of integers(to request from the cache), calculate the
number of cache misses using the LRU cache algorithm. A cache miss occur when the requested
integer does not exist in the cache.
Initially, the cache is empty. The input to the function LruCountMiss shall consist of an
integer max_cache_size, an array pages and its length len.
The function should return an integer for the number of cache misses using
the LRU cache algorithm. Assume that the array pages always has pages numbered
from 1 to 50.
TESTCASES:
TESTCASE1:
INPUT:
3,[7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0],16
EXPECTED RETURN VALUE:
11
TESTCASE 2:
INPUT:
2,[2,3,1,3,2,1,4,3,2],9
EXPECTED RETURN VALUE:
8
EXPLANATION:
The following page numbers are missed one after the other 2,3,1,2,1,4,3,2.This results
in 8 page misses.
Sample CODE:
int lruCountMiss(int max_cache_size, int *pages,int len)
{/
/write tour code
}
#include<iostream>
using namespace std;
for (int i=0;i<len;i++){ //for loop for all the value from list one by one
for(int j=0;j<max_cache_size;j++){ //loop to check the cache value, if it matches the value
if (pages[i]==cache[j]){
for (int k=i; k<max_cache_size;k++){ /*if the value is already present in the cache then shi ing values from
the present value.*/
cache[i]=cache[i+1];
}
cache[max_cache_size-1]=pages[i]; //upda ng the last value of cache
break;
}
else if(j==(max_cache_size-1)){
for (int l=0; l<max_cache_size;l++){ /*if the value is not present in the cache then shi ing values from
star ng.*/
cache[l]=cache[l+1];
}
cache[max_cache_size-1]=pages[i];//upda ng the last value of cache
miss++;
}
}}
return miss;}//returning the Miss
if(head!=NULL)
{
while(fast_ptr != NULL && fast_ptr->next != NULL)
{
fast_ptr = fast_ptr->next->next;
slow_ptr = slow_ptr->next;
}
printf("The middle element is [%d]\n\n", slow_ptr->data);
}
}
return0;
}
Run on IDE
Output:
5->NULL
The middle element is [5]
4->5->NULL
The middle element is [5]
3->4->5->NULL
The middle element is [4]
2->3->4->5->NULL
The middle element is [4]
1->2->3->4->5->NULL
The middle element is [3]
Method 3:
Initialize mid element as head and initialize a counter as 0. Traverse the list from head,
while traversing increment the counter and change mid to mid->next whenever the
counter is odd. So the mid will move only half of the total length of the list.
Thanks to Narendra Kangralkar for suggesting this method.
#include<stdio.h>
#include<stdlib.h>
while(head != NULL)
{
/* update mid, when 'count' is odd number */
if(count & 1)
mid = mid->next;
++count;
head = head->next;
}
return0;
}
5->NULL
The middle element is [5]
4->5->NULL
The middle element is [5]
3->4->5->NULL
The middle element is [4]
2->3->4->5->NULL
The middle element is [4]
5
1->2->3->4->5->NULL
The middle element is [3]
int merge[MAX],i,n;
partition(merge,0,n-1);
return 0;
}
int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}
/* pull off the front node of the source and put it in dest */
void MoveNode(struct Node** destRef, struct Node** sourceRef);
tail = tail->next;
}
return(dummy.next);
}
/* UTILITY FUNCTIONS */
/* MoveNode() function takes the node from the front of the
source, and move it to the front of the dest.
It is an error to call this with the source list empty.
push(&b, 20);
push(&b, 3);
push(&b, 2);
return 0;
}
Output :
Merged Linked List is:
2 3 5 10 15 20
Following C program ask to the user to enter a string to find the vowel present in that string then
delete the vowel and form the new string, then display the result on the screen:
/* C Program - Delete Vowels from String */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[20];
int len, i, j;
printf("Enter a string : ");
gets(str);
len=strlen(str);
for(i=0; i<len; i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U')
{
for(j=i; j<len; j++)
{
str[j]=str[j+1];
}
len--;
}
}
printf("After deleting the vowels, the string will be : %s",str);
getch();
}
Print and count all the numbers which are less than a given key element from a given array
/* C Program - Delete Vowels from String */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[20];
int len, i, j;
printf("Enter a string : ");
gets(str);
len=strlen(str);
for(i=0; i<len; i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U')
{
for(j=i; j<len; j++)
{
str[j]=str[j+1];
}
len--;
}
}
printf("After deleting the vowels, the string will be : %s",str);
getch();
}
Remove vowels string using pointers in C:
1. #include<stdio.h>
2. #include<stdlib.h>
3. #include<string.h>
4. #define TRUE 1
5. #define FALSE 0
6.
7. int check_vowel(char);
8.
9. main()
10. {
11. char string[100], *temp, *pointer, ch, *start;
12.
13. printf("Enter a string\n");
14. gets(string);
15.
16. temp = string;
17. pointer = (char*)malloc(100);
18.
19. if( pointer == NULL )
20. {
21. printf("Unable to allocate memory.\n");
22. exit(EXIT_FAILURE);
23. }
24.
25. start = pointer;
26.
27. while(*temp)
28. {
29. ch = *temp;
30.
31. if ( !check_vowel(ch) )
32. {
33. *pointer = ch;
34. pointer++;
35. }
36. temp++;
37. }
38. *pointer = '\0';
39.
40. pointer = start;
41. strcpy(string, pointer); /* If you wish to convert original string */
42. free(pointer);
43.
44. printf("String after removing vowel is \"%s\"\n", string);
45.
46. return 0;
47. }
48.
49. int check_vowel(char a)
50. {
51. if ( a >= 'A' && a <= 'Z' )
52. a = a + 'a' - 'A';
53.
54. if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
55. return TRUE;
56.
57. return FALSE;
58. }
Program or code for prime numbers between 1 to n in c
language
#include<stdio.h>
int main(){
int num,i,count,n;
prin ("Enter max range: ");
scanf("%d",&n);
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
return 0;
}
Sample output:
Enter max range: 50
2 3 5 7 11 13