Interview Program Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

A system that can run multiple concurrent jobs on a single CPU have a process of choosing which task hast

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;

int lruCountMiss(int max_cache_size, int *pages,int len)


{
int miss=0,cache[max_cache_size]; //variable miss and cache declared
for (int i=0;i<max_cache_size;i++){ /*this is for clearing value of cache i.e. to empty cache. I used any value
here*/
cache[i]=0x87787;
}

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

int main(){ //main func on


cout<< "This is the Least Recently Used(LRU) cache algorithm.n By HIMANSHU SINGH from
mindxmaster.com\n";
cout<< "We are checking two cases.\n"<<"Case 1:t"<<"Array values are [7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0] nand
cache size= 3; and total values to check are 16: n We got the miss. ";
int days,pages[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0}; //array to pass through func on
int *cellsptr=pages; //crea ng array values to pointer
cout<<"Miss =t"<<lruCountMiss(3,cellsptr,16);//passing to func on
cout<<"nnCase 2:t"<<"Array values are [2,3,1,3,2,1,4,3,2] nand cache size= 2; and total values to check are
9: n We got the miss. ";
int pages2[]={2,3,1,3,2,1,4,3,2};
int *cellsptr2=pages2; //crea ng array values to pointer
cout<<"Miss =t"<<lruCountMiss(2,cellsptr2,9);//passing to func on
return 0;
}

Find the middle of a given linked list in C and Java


#include<stdio.h>
#include<stdlib.h>

/* Link list node */


structNode
{
intdata;
structNode* next;
};

/* Function to get the middle of the linked list*/


voidprintMiddle(structNode *head)
{
structNode *slow_ptr = head;
structNode *fast_ptr = head;

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);
}
}

voidpush(structNode** head_ref, intnew_data)


{
/* allocate node */
structNode* new_node =
(structNode*) malloc(sizeof(structNode));

/* put in the data */


new_node->data = new_data;

/* link the old list off the new node */


new_node->next = (*head_ref);

/* move the head to point to the new node */


(*head_ref) = new_node;
}
// A utility function to print a given linked list
voidprintList(structNode *ptr)
{
while(ptr != NULL)
{
printf("%d->", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
}

/* Drier program to test above function*/


intmain()
{
/* Start with the empty list */
structNode* head = NULL;
inti;

for(i=5; i>0; i--)


{
push(&head, i);
printList(head);
printMiddle(head);
}

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>

/* Link list node */


structnode
{
intdata;
structnode* next;
};

/* Function to get the middle of the linked list*/


voidprintMiddle(structnode *head)
{
intcount = 0;
structnode *mid = head;

while(head != NULL)
{
/* update mid, when 'count' is odd number */
if(count & 1)
mid = mid->next;

++count;
head = head->next;
}

/* if empty list is provided */


if(mid != NULL)
printf("The middle element is [%d]\n\n", mid->data);
}

voidpush(structnode** head_ref, intnew_data)


{
/* allocate node */
structnode* new_node =
(structnode*) malloc(sizeof(structnode));

/* put in the data */


new_node->data = new_data;

/* link the old list off the new node */


new_node->next = (*head_ref);

/* move the head to point to the new node */


(*head_ref) = new_node;
}
// A utility function to print a given linked list
voidprintList(structnode *ptr)
{
while(ptr != NULL)
{
printf("%d->", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
}

/* Drier program to test above function*/


intmain()
{
/* Start with the empty list */
structnode* head = NULL;
inti;

for(i=5; i>0; i--)


{
push(&head, i);
printList(head);
printMiddle(head);
}

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]

C program to implement merge sort using func on and pointers


#include<stdio.h>
#define MAX 50

void mergeSort(int arr[],int low,int mid,int high);


void partition(int arr[],int low,int high);
int main(){

int merge[MAX],i,n;

printf("Enter the total number of elements: ");


scanf("%d",&n);

printf("Enter the elements which to be sort: ");


for(i=0;i<n;i++){
scanf("%d",&merge[i]);
}

partition(merge,0,n-1);

printf("After merge sorting elements are: ");


for(i=0;i<n;i++){
printf("%d ",merge[i]);
}

return 0;
}

void partition(int arr[],int low,int high){

int mid;

if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}

void mergeSort(int arr[],int low,int mid,int 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];
}
}

Merge two sorted linked lists


C/C++ program to merge two sorted linked lists */
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

/* Link list node */


struct Node
{
int data;
struct Node* next;
};

/* pull off the front node of the source and put it in dest */
void MoveNode(struct Node** destRef, struct Node** sourceRef);

/* Takes two lists sorted in increasing order, and splices


their nodes together to make one big sorted list which
is returned. */
struct Node* SortedMerge(struct Node* a, struct Node* b)
{
/* a dummy first node to hang the result on */
struct Node dummy;

/* tail points to the last result node */


struct Node* tail = &dummy;

/* so tail->next is the place to add new nodes


to the result. */
dummy.next = NULL;
while (1)
{
if (a == NULL)
{
/* if either list runs out, use the
other list */
tail->next = b;
break;
}
else if (b == NULL)
{
tail->next = a;
break;
}
if (a->data <= b->data)
MoveNode(&(tail->next), &a);
else
MoveNode(&(tail->next), &b);

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.

Before calling MoveNode():


source == {1, 2, 3}
dest == {1, 2, 3}

Affter calling MoveNode():


source == {2, 3}
dest == {1, 1, 2, 3} */
void MoveNode(struct Node** destRef, struct Node** sourceRef)
{
/* the front source node */
struct Node* newNode = *sourceRef;
assert(newNode != NULL);

/* Advance the source pointer */


*sourceRef = newNode->next;

/* Link the old dest off the new node */


newNode->next = *destRef;

/* Move dest to point to the new node */


*destRef = newNode;
}

/* Function to insert a node at the beginging of the


linked list */
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));

/* put in the data */


new_node->data = new_data;

/* link the old list off the new node */


new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}

/* Function to print nodes in a given linked list */


void printList(struct Node *node)
{
while (node!=NULL)
{
printf("%d ", node->data);
node = node->next;
}
}

/* Drier program to test above functions*/


int main()
{
/* Start with the empty list */
struct Node* res = NULL;
struct Node* a = NULL;
struct Node* b = NULL;

/* Let us create two sorted linked lists to test


the functions
Created lists, a: 5->10->15, b: 2->3->20 */
push(&a, 15);
push(&a, 10);
push(&a, 5);

push(&b, 20);
push(&b, 3);
push(&b, 2);

/* Remove duplicates from linked list */


res = SortedMerge(a, b);

printf("Merged Linked List is: \n");


printList(res);

return 0;
}
Output :
Merged Linked List is:
2 3 5 10 15 20

C Programming Code to Delete Vowels from String

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;
}
}

if(count==0 && num!= 1)


prin ("%d ",num);
}

return 0;
}

Sample output:
Enter max range: 50
2 3 5 7 11 13

You might also like