Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The American multinational technology corporation Amazon focuses on a wide range of industries, such as digital streaming, cloud computing, artificial intelligence, and e-commerce. Along with Google, Apple, Microsoft, and Facebook, it is regarded as one of the "Big Five" corporations in the American information technology sector.


Amazon is recognized for its massive industry disruptions brought about by technology innovation and size. Being the largest online retailer, provider of AI assistants, and cloud computing platform in the world, Amazon employs over six lakh people, of whom more than 50,000 are Indians. It is currently the second-biggest private employer in the United States and the largest Internet corporation in the world by revenue. Amazon has the largest global brand valuation as a result.

It is very evident that many people desire to work at Amazon and be a part of its evergreen culture given the variety of services it provides. But what exactly is Amazon's recipe for enormous success? Well, if you were to ask every Amazon employee this question, we are rather confident that they would all respond in unison with: "The Leadership Principles of Amazon."

Interview Process
  1. Recruiter Connect: Best way to get noticed by Amazon recruiters is to maintain a good Linkedin profile and message recruiters. The candidate can also apply on the Amazon job portal but it is suggested that they also get a referral from an Amazon employee.
  2. Interview Rounds: Amazon conducts four interview rounds alongside an initial coding test. The coding test consists of DS/Algo problems. The first round is an HR round where they ask behavioral questions along with Computer Science theory questions to the candidate. The next three rounds focus solely on DS/Algo.
  3. After Interviews: The recruiter contacts the candidate after these rounds and tells the verdict. They also look at the candidate’s leadership principles along with technical skills.
  4. Hired: Once the team and you both are comfortable and ready to start, the offer letter is prepared and shared with you by the recruiters and you are HIRED!
Interview Rounds
  1. HR Round(1 Round) This is when they ask computer science theory and behavioural questions to the candidate. The questions may enquire about the candidate’s experience at previous companies and conflicts the candidate might have faced with colleagues/managers.
  2. Data Structures and Algorithms Rounds(3 Rounds) The candidate is asked DS/Algo problems where production ready code might be expected from the candidate. It is not out of the realm of possibility to face minor behavioural questions here as well. The problems range from easy to hard but they are not the sole deciding factor for the final offer. Leadership principles also come into play here. The interviews are conducted on Amazon Chime.

Google Interview Questions

There are two sorts of interviews, phone/hangout interviews and on-site interviews, according to Google's official career page. An excerpt from their official page is provided below.

For candidates for software engineering positions, we are interested in your coding abilities, technical specialties, knowledge of tools or programming languages, and broad understanding of concepts like data structures and algorithms. We prefer to challenge one other's thinking and discover new perspectives, so there is usually some back and forth in these discussions, just like there is on the job. So be ready to thoroughly discuss your solutions. Find the greatest solution by pushing your own limits—that's probably how you think anyway.



Google Questions from Medium to Hard level are as follows:

Easy Level

  1. Find all triplets with zero sum
  2. Generate all binary strings from given pattern
  3. Count of strings that can be formed using a, b and c under given constraints
  4. Find largest word in dictionary by deleting some characters of given string
  5. Find subarray with given sum | Set 1 (Nonnegative Numbers)
  6. Find the longest substring with k unique characters in a given string
  7. Find the two non-repeating elements in an array of repeating elements
  8. Flood fill Algorithm – how to implement fill() in paint?
  9. Meta Strings (Check if two strings can become same after a swap in one string)
  10. Print all Jumping Numbers smaller than or equal to a given value
  11. Sum of all the numbers that are formed from root to leaf paths
  12. The Celebrity Problem
  13. Unbounded Knapsack (Repetition of items allowed)

Medium Level

  1. Backtracking | Set 7 (Sudoku)
  2. Boggle | Set 2 (Using Trie)
  3. Check if a Binary Tree contains duplicate subtrees of size 2 or more
  4. Dynamic Programming | Set 33 (Find if a string is interleaved of two other stri
  5. Connect nodes at same level
  6. Count BST nodes that lie in a given range
  7. Dynamic Programming | Set 11 (Egg Dropping Puzzle)
  8. Dynamic Programming | Set 28 (Minimum insertions to form a palindrome)
  9. Dynamic Programming | Set 31 (Optimal Strategy for a Game)
  10. Dynamic Programming | Set 32 (Word Break Problem)
  11. Find four elements that sum to a given value | Set 2 ( O(n^2Logn) Solution)
  12. Given a matrix of ‘O’ and ‘X’, replace ‘O’ with ‘X’ if surrounded by ‘X’
  13. How to print maximum number of A’s using given four keys
  14. Inplace rotate square matrix by 90 degrees | Set 1
  15. Maximum absolute difference between sum of two contiguous sub-arrays
  16. Merge two BSTs with limited extra space
  17. Merge Overlapping Intervals
  18. Modular Exponentiation (Power in Modular Arithmetic)
  19. Paper Cut into Minimum Number of Squares | Set 2
  20. Sum of bit differences among all pairs

Hard Level

  1. Allocate minimum number of pages
  2. Given an array arr[], find the maximum j – i such that arr[j] > arr[i]
  3. Given a sorted dictionary of an alien language, find order of characters
  4. Hungarian Algorithm for Assignment Problem | Set 1 (Introduction)
  5. Implement LRU Cache
  6. Length of the longest valid substring
  7. Median in a stream of integers (running integers)
  8. Sum of bit differences among all pairs
  9. Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
  10. Word Break Problem using Backtracking

Rotate matrix by 90 degrees

 Set-1

In this given set-1 the problem statement is like Given a square matrix, turn it by 90 degrees in an anti-clockwise direction without using any extra space.




Input:
Matrix:        1  2  3  4 
               5  6  7  8 
               9 10 11 12 
              13 14 15 16 

Output:        4  8 12 16 
               3  7 11 15 
               2  6 10 14 
               1  5  9 13 

Follow the given steps to solve the problem:

  1. There are N/2 squares or cycles in a matrix of side N. Process a square one at a time. Run a loop to traverse the matrix a cycle at a time, i.e loop from 0 to N/2 – 1, loop counter is i
  2. Consider elements in group of 4 in current square, rotate the 4 elements at a time. So the number of such groups in a cycle is N – 2*i.
  3. So run a loop in each cycle from x to N – x – 1, loop counter is y
  4. The elements in the current group is (x, y), (y, N-1-x), (N-1-x, N-1-y), (N-1-y, x), now rotate the these 4 elements, i.e (x, y) <- (y, N-1-x), (y, N-1-x)<- (N-1-x, N-1-y), (N-1-x, N-1-y)<- (N-1-y, x), (N-1-y, x)<- (x, y)
  5. Print the matrix.

Code Implementation

// cskecode C++ program to rotate a matrix
// by 90 degrees
#include <bits/stdc++.h>
#define N 4
using namespace std;

// An Inplace function to
// rotate a N x N matrix
// by 90 degrees in
// anti-clockwise direction
void rotateMatrix(int mat[][N])
{
// Consider all squares one by one
for (int x = 0; x < N / 2; x++) {
// Consider elements in group
// of 4 in current square
for (int y = x; y < N - x - 1; y++) {
// Store current cell in
// temp variable
int temp = mat[x][y];

// Move values from right to top
mat[x][y] = mat[y][N - 1 - x];

// Move values from bottom to right
mat[y][N - 1 - x] = mat[N - 1 - x][N - 1 - y];

// Move values from left to bottom
mat[N - 1 - x][N - 1 - y] = mat[N - 1 - y][x];

// Assign temp to left
mat[N - 1 - y][x] = temp;
}
}
}

// Function to print the matrix
void displayMatrix(int mat[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
cout << endl;
}

/* Driver code */
int main()
{
// Test Case 1
int mat[N][N] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };

// Function call
rotateMatrix(mat);

// Print rotated matrix
displayMatrix(mat);

return 0;
}

Time Complexity: O(N2), where n is the side of the array. A single traversal of the matrix is needed.
Auxiliary Space: O(1). As a constant space is needed

Set-2

Inplace rotate square matrix by 90 degrees by transposing and reversing the matrix:
Follow the given steps to solve the problem:

  1. Reverse every individual row of the matrix
  2. Perform Transpose of the matrix
Code implementation

// Cskecode C++ program to rotate a matrix
// by 90 degrees
#include <bits/stdc++.h>
using namespace std;
#define N 4

// An Inplace function to
// rotate a N x N matrix
// by 90 degrees in
// anti-clockwise direction
void rotateMatrix(int mat[][N])
{ // REVERSE every row
for (int i = 0; i < N; i++)
reverse(mat[i], mat[i] + N);

// Performing Transpose
for (int i = 0; i < N; i++) {
for (int j = i; j < N; j++)
swap(mat[i][j], mat[j][i]);
}
}

// Function to print the matrix
void displayMatrix(int mat[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
cout << endl;
}

/* Driver code */
int main()
{
int mat[N][N] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };

// Function call
rotateMatrix(mat);

// Print rotated matrix
displayMatrix(mat);

return 0;
}


Time Complexity: O(N2) + O(N2)  where N is the size of the array.
Auxiliary Space: O(1). As a constant space is needed

Amazon Interview Questions

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The American multinational technology corporation Amazon focuses on a wide range of industries, such as digital streaming, cloud computing, artificial intelligence, and e-commerce. Along with Google, Apple, Microsoft, and Facebook, it is regarded as one of the "Big Five" corporations in the American information technology sector.


Amazon is recognized for its massive industry disruptions brought about by technology innovation and size. Being the largest online retailer, provider of AI assistants, and cloud computing platform in the world, Amazon employs over six lakh people, of whom more than 50,000 are Indians. It is currently the second-biggest private employer in the United States and the largest Internet corporation in the world by revenue. Amazon has the largest global brand valuation as a result.

It is very evident that many people desire to work at Amazon and be a part of its evergreen culture given the variety of services it provides. But what exactly is Amazon's recipe for enormous success? Well, if you were to ask every Amazon employee this question, we are rather confident that they would all respond in unison with: "The Leadership Principles of Amazon."

Interview Process

  1. Recruiter Connect: Best way to get noticed by Amazon recruiters is to maintain a good Linkedin profile and message recruiters. The candidate can also apply on the Amazon job portal but it is suggested that they also get a referral from an Amazon employee.
  2. Interview Rounds: Amazon conducts four interview rounds alongside an initial coding test. The coding test consists of DS/Algo problems. The first round is an HR round where they ask behavioral questions along with Computer Science theory questions to the candidate. The next three rounds focus solely on DS/Algo.
  3. After Interviews: The recruiter contacts the candidate after these rounds and tells the verdict. They also look at the candidate’s leadership principles along with technical skills.
  4. Hired: Once the team and you both are comfortable and ready to start, the offer letter is prepared and shared with you by the recruiters and you are HIRED!

Interview Rounds

HR Round(1 Round) 

This is when they ask computer science theory and behavioural questions to the candidate. The questions may enquire about the candidate’s experience at previous companies and conflicts the candidate might have faced with colleagues/managers.

Data Structures and Algorithms Rounds(3 Rounds) 

The candidate is asked DS/Algo problems where production ready code might be expected from the candidate. It is not out of the realm of possibility to face minor behavioural questions here as well. The problems range from easy to hard but they are not the sole deciding factor for the final offer. Leadership principles also come into play here. The interviews are conducted on Amazon Chime.

Questions

  1. K largest elements from a big file or array.
  2. Find a triplet a, b, c such that a2 = b2 + c2. Variations of this problem like find a triplet with sum equal to 0. Find a pair with given sum. All such questions are efficiently solved using hashing. – Practice here
  3. Binary tree traversal questions like left view, right view, top view, bottom view, maximum of a level, minimum of a level, children sum property, diameter etc.
  4. Convert a Binary tree to DLL – Practice here
  5. Lowest Common ancestor in a Binary Search Tree and Binary Tree.
  6. Implement a stack with push(), pop() and min() in O(1) time.
  7. Reverse a linked list in groups of size k – Practice here
  8. Given two numbers represented by two linked lists, write a function that returns sum list – Practice here
  9. Rotate a matrix by 90 degree.
  10. Stock span problem
  11. Next greater element
  12. Some Dynamic Programming problems like:
  13. Maximum sum subarray such that no elements are consecutive – Practice here
  14. Edit distance
  15. Assembly line scheduling
  16. Why Amazon?

10.Write a program to calculate factorial of a number using thread library

Write a program to calculate factorial of a number using thread library.

//cskecode
//cskecode.blogspot.com
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<pthread.h>
int fact;
void *run(void *param);
int main(int argc, char *argv[])
{
pthread_t tid;
pthread_attr_t attr;
if(argc !=2)
{
printf("Error !");
return 1;
}
if(atoi(argv[1])<O)
{
printf("no. Should be +ive");
return 1;
}
pthread_attr_init(&attr);
pthread_create(&tid, &attr,run, argv[1]);
pthread_join(tid,NULL);
printf("fact= %d in", fact);
}
void *run(void *param)
{
int i, upper;
fact=1;
upperratoi(param);
if(upper>0)
for(i=1;i<=upper; ++i)
fact*=i;
pthread_exit(0);
}
//cskecode.blogspot.com

Output:


9.A program to calculate sum of n numbers using thread library

A program to calculate sum of n numbers using thread library. 

cskecode

//cskecode.blogspot.com
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<pthread.h>
int sum;
void *run(void *param);
int main(int argc, char *argv[]).
{
pthread_t tid;
pthread_attr_t attr;
if(argc !=2)
{
printf("Error !");
return 1;
}
if(atoi(argv[1])<0)
{
printf("no. Should be +ive");
return 1;
}
pthread_attr_init(&attri
pthread_create(&tid,&attr.run.arguri):
pthread_join(tid, NULL);
printf("Sum = %d in", sum);
}
void *run(void *param)
{
int i,upper;
sum=0 ;
upper=atoi(param);
if(upper>0)
{
for(i=1;i<=upper; ++i)
SUM+=i:
pthread_exit(0);
}
}
//cskecode.blogspot.com

Output:


8.Write program to implement SJF scheduling algorithm (Preemptive)

Write program to implement SJF scheduling algorithm (Preemptive)

//cskecode

//cskecode.blogspot.com
#include<iostream>
using namespace std;
class sched{
public:
int n,bt[10],at[10],tat[10],wt[10],rt[10],finish[10],twt,ttat,total;
void readData();
void computeSRT();
void Init();
void dispTime();
int getNextProcess(int);
};

void sched::readData()
{
cout<<"Enter no. of processes\n";
cin>>n;
cout<<"Enter the burst times in order :\n";
for(int i=0;i<n;i++)
cin>>bt[i];
cout<<"Enter the arrival times in order:\n";
for(int i=0;i<n;i++)
cin>>at[i];
}

void sched::Init()
{
total=0;
twt=0;
ttat=0;
for(int i=0; i<n; i++)
{
 rt[i]=bt[i];
finish[i]=0;
wt[i]=0;
tat[i]=0;
total+=bt[i];
}
}

void sched::computeSRT()
{
 readData();
Init();
int time,next=0,old,i;
cout<<"Gantt Chart\n ";
for(time=0;time<total;time++)
{
old=next;
next=getNextProcess(time);
if(old!=next || time==0) 
{
cout<<"("<<time<<")|==P"<<next+1<<"==|";
}
rt[next]=rt[next]-1;
if(rt[next]==0) 
{
finish[next]=1;
}
for(i=0;i<n;i++)
if(i!=next && finish[i]==0 && at[i]<=time)
wt[i]++;
}
cout<<"("<<total<<")"<<endl;
for(i=0;i<n;i++)
if(!finish[i]) {cout<<"Scheduling failed, cannot continue\n"; return;}
dispTime();
}

int sched::getNextProcess(int time)

int i,low;
for(i=0;i<n;i++)
if(finish[i]==0){low=i; break; }
for(i=0;i<n;i++)
if(finish[i]!=1)
if(rt[i]<rt[low] && at[i]<=time)
low=i;
return low;
}

void sched::dispTime()
{
for(int i=0;i<n;i++)
{
twt+=wt[i];
tat[i]=wt[i]+bt[i];
ttat+=tat[i];
cout<<"Waiting time for P"<<(i+1)<<" = "<<wt[i]<<", Turnaround time = "<<tat[i]<<endl;
}
cout<<"Avg Waiting time = "<<(double)twt/n<<" and Avg Turnaround time = "<<(double)ttat/n<<endl;
cout<<"Scheduling complete\n";
}

int main()
{
sched s;
s.computeSRT();
}
//cskecode.blogspot.com


Output:

8.Write program to implement SJF scheduling algorithm (Non-Preemptive)

Write program to implement SJF scheduling algorithm (Non-Preemptive)


//cskecode

#include<iostream>
#include<stdio.h>
using namespace std;
class sjf
{      
public:
int burst_tm,arv_tm,wait_tm,temp;
char pid;
sjf()
{
temp=0;
//status=0;
}
};
int main()
{
int n,sum=0,carry=0;
float avg_wait_tm=0,avg_around_tm=0;
cout<<"Enter the no. of processes:\n";
cin>>n;
sjf temp;
sjf *f=new sjf[n];
for( int i=0;i<n;i++)
{
cout<<"Enter the process id:\n";
cin>>f[i].pid;
cout<<"Enter the burst timing:\n";
cin>>f[i].burst_tm;
cout<<"Enter the arrival timing:\n";
cin>>f[i].arv_tm;
}
   
int k1=0,flag=1;
while(k1<n)
{
if(f[k1].arv_tm==0)
{
flag=0;
break;
}
k1++;
}
if(flag==1)
{
cout<<"enter all values of arival time as no arrival time is 0\n";
return 1;
}
   
for(int k=0;k<n;k++)
{
temp=f[k];
for(int i=k+1;i<n;i++)
{
if(temp.arv_tm>f[i].arv_tm)
{
temp=f[i];
f[i]=f[k];
f[k]=temp;
}
//cout<<"f[0]: "<<f[0].pid<<endl;
}
}
   
for(int k=0;k<n;k++)
{
cout<<"pid: "<<f[k].pid<<endl<<"Burst time: "<<f[k].burst_tm<<endl<<"arrival time: "<<f[k].arv_tm<<endl<<"Waiting time: "<<f[k].wait_tm<<endl;
                   
}
   
cout<<"Entered info is as follows:\n";
carry=f[0].arv_tm;
int i=0;
for(int k=1;k<n;k++)
{
if(f[k].arv_tm==carry)
{
if(f[i].burst_tm>f[k].burst_tm)
i=k;
}
else
{
cout<<"carry break\n";
break;
}
}
   
int j,tem=0,k,b;
do
{
j=0;
if((f[i].arv_tm<=sum)&&(f[i].temp!=1))
{
f[i].wait_tm=sum-f[i].arv_tm;
f[i].temp=1;
sum+=f[i].burst_tm;
}
else
{
if(tem==0)
{
sum=f[i].arv_tm;
tem=1;
}
f[i].wait_tm=sum-f[i].arv_tm;
f[i].temp=1;
sum+=f[i].burst_tm;
}
flag=1;
for(int k=0;k<n;k++)
{
if(f[k].arv_tm<=sum)
{
 //flag=0;
if(f[k].temp==1)
j++;
else if((f[k].burst_tm<f[i].burst_tm)||(f[i].temp==1))
{
flag=0;
i=k;
}
}
}
                                   
if((flag==1)&&(j!=n-1))
{
for(b=i;b<n;b++)
if(!f[b].temp)
{
carry=f[b].arv_tm;
i=b;
break;
}
for(int t=b+1;t<n;t++)
{
if(f[t].arv_tm==carry)
{
if(f[t].burst_tm<f[i].burst_tm)
i=t;
}
else
break;
}
}
}while(j!=n);
                                                                                                   
                                             
for(k=0;k<n;k++)
{
cout<<"pid: "<<f[k].pid<<endl<<"Burst time: "<<f[k].burst_tm<<endl<<"arrival time: "<<f[k].arv_tm<<endl<<"Waiting time: "<<f[k].wait_tm<<endl;
avg_wait_tm+=f[k].wait_tm;
avg_around_tm+=f[k].wait_tm+f[k].burst_tm;
                   
                   
}
avg_around_tm/=n;
avg_wait_tm/=n;
cout<<"average turn around time:\n"<<avg_around_tm<<endl<<"average waiting time:\n"<<avg_wait_tm<<endl;
   
//cout<<"average turn around time:\n"<<avg_around_tm<<endl<<"average waiting time:\n"<<avg_wait_tm<<endl;
getchar();
getchar();
getchar();
   
return 0;
}


Output:

8.Write program to implement SJF scheduling algorithm

Write program to implement SJF scheduling algorithm (IF Arrival time is 0)

//cskecode

#include<iostream>
#include<stdio.h>
using namespace std;
class sjf
{      
public:
int burst_tm,arv_tm,wait_tm,temp;
char pid;
};

int main()
{
int n,sum=0,carry;
cout<<"Enter the no. of processes:\n";
cin>>n;
sjf temp;
sjf *f=new sjf[n];
for( int i=0;i<n;i++)
{
cout<<"Enter the process id:\n";
cin>>f[i].pid;
cout<<"Enter the burst timing:\n";
cin>>f[i].burst_tm;
cout<<"Enter the arrival timing:\n";
cin>>f[i].arv_tm;
}

int k=0,flag=1;
while(k!=0)
{
if(f[k].arv_tm==0)
{
flag=0;
break;
}
k++;
}
if(!flag)
{
cout<<"enter all values of arival time as no arrival time is 0\n";
return 1;
}
for(k=0;k<n;k++)
{
temp=f[k];
for(int i=k+1;i<n;i++)
{
if(temp.burst_tm>f[i].burst_tm)
{
temp=f[i];
f[i]=f[k];
f[k]=temp;
}
}
}
cout<<"Entered info is as follows:\n";
float avg_wait_tm=0,avg_around_tm=0;
for(k=0;k<n;k++)
{                                            
f[k].wait_tm=sum-f[k].arv_tm;
if(f[k].wait_tm<0)
f[k].wait_tm=0;
if(f[k].arv_tm>(f[k-1].burst_tm+f[k-1].arv_tm))
{
carry=f[k].arv_tm-sum;
sum+=(f[k].burst_tm+carry);
}
else
sum+=f[k].burst_tm;
avg_wait_tm+=f[k].wait_tm;
avg_around_tm+=f[k].wait_tm+f[k].burst_tm;
}
avg_around_tm/=n;
avg_wait_tm/=n;

for(k=0;k<n;k++)
{
cout<<"pid: "<<f[k].pid<<endl<<"Burst time: "<<f[k].burst_tm<<endl<<"arrival time: "<<f[k].arv_tm<<endl<<"Waiting time: "<<f[k].wait_tm<<endl;

cout<<"This is my Blog: ducskecode.blogspot.com" \n";

}
cout<<"average turn around time:\n"<<avg_around_tm<<endl<<"average waiting time:\n"<<avg_wait_tm<<endl;

return 0;
}

Output:



7.Write a program to implement round robin scheduling algorithm.

Write a program to implement round robin scheduling algorithm.


#include<iostream>
using namespace std;
class sched
{
public:
int n,bt[10],at[10],tat[10],wt[10],rt[10],finish[10],twt,ttat,total;
void readData();
void Init();
void dispTime();
void computeRR();
};

void sched::readData()
{
cout<<"Enter no. of processes\n";
cin>>n;
cout<<"Enter the burst times in order :\n";
for(int i=0;i<n;i++)
cin>>bt[i];
cout<<"Enter the arrival times in order:\n";
for(int i=0;i<n;i++)
cin>>at[i];
}

void sched::Init()
{
total=0;
twt=0;
ttat=0;
for(int i=0; i<n; i++)
{
rt[i]=bt[i];
finish[i]=0;
wt[i]=0;
tat[i]=0;
total+=bt[i];
}
}

void sched::dispTime()
{
for(int i=0;i<n;i++)
{
twt+=wt[i];
tat[i]=wt[i]+bt[i];
ttat+=tat[i];
cout<<"Waiting time for P"<<(i+1)<<" = "<<wt[i]<<", Turnaround time = "<<tat[i]<<endl;
}

cout<<"Avg Waiting time = "<<(double)twt/n<<" and Avg Turnaround time = "<<(double)ttat/n<<endl;
cout<<"Scheduling complete\n";
}

void sched::computeRR(){
readData();
Init();
int time,j,q,i,dec=0;
cout<<"Enter the time quantum:\n";
cin>>q;
cout<<"Gantt Chart\n ";
for(time=0;time<total;)
{
for(i=0;i<n;i++)
{
if(at[i]<=time && finish[i]==0)
{
cout<<"("<<time<<")|==P"<<(i+1)<<"==|";
if(rt[i]<q)  {
dec=rt[i];
}
else {dec=q;}
rt[i]=rt[i]-dec;
if(rt[i]==0)
finish[i]=1;
for(j=0;j<n;j++)
if(j!=i && finish[j]==0 && at[j]<=time)
wt[j]+=dec;
time=time+dec;
}
}
}
cout<<"("<<total<<")"<<endl;
dispTime();
}
int main()
{
sched t;
int ch;
t.computeRR();
}

Output:



6.Write a program to implement fcfs scheduling algorithm.

Write a program to implement fcfs scheduling algorithm.

//ducskecode

//ducskecode.blogspot.com
#include<iostream>
#include<stdio.h>
using namespace std;
class fcfs
{  
public:
int burst_tm,arv_tm,wait_tm;
char pid;
};
int  main()
{
int n,sum=0,carry;
cout<<"Enter the no. of processes:\n";
cin>>n;
fcfs temp;
fcfs *f=new fcfs[n];
for( int i=0;i<n;i++)
{
cout<<"Enter the process id:\n";
cin>>f[i].pid;
cout<<"Enter the burst timing:\n";
cin>>f[i].burst_tm;
cout<<"Enter the arrival timing:\n";
cin>>f[i].arv_tm;
//system("cls");
}
int k=0,flag=1;
while(k!=0)
{
if(f[k].arv_tm==0)
{
flag=0;
break;
}
k++;
}
if(!flag)
{
cout<<"enter all values of arival time as no arrival time is 0\n";
return 1;
}
for(k=0;k<n;k++)
{
temp=f[k];
for(int i=k+1;i<n;i++)
{
if(temp.arv_tm>f[i].arv_tm)
{
temp=f[i];
f[i]=f[k];
f[k]=temp;
}
}
}
cout<<"Entered info is as follows:\n";
int avg_wait_tm=0,avg_around_tm=0;
for(k=0;k<n;k++)
{                                            
f[k].wait_tm=sum-f[k].arv_tm;
if(f[k].wait_tm<0)
f[k].wait_tm=0;
                                   
sum+=f[k].burst_tm;
avg_wait_tm+=f[k].wait_tm;
avg_around_tm+=f[k].wait_tm+f[k].burst_tm;
}
avg_around_tm/=n;
avg_wait_tm/=n;
for(k=0;k<n;k++)
{
cout<<"pid: "<<f[k].pid<<endl<<"Burst time: "<<f[k].burst_tm<<endl<<"arrival time:"<<f[k].arv_tm<<endl<<"Waiting time: "<<f[k].wait_tm<<endl;
 }
cout<<"average turn around time:\n"<<avg_around_tm<<endl<<"average waiting time:\n"<<avg_wait_tm<<endl;
return 0;
}

Output:


5.A program to copy file using system call

A program to copy file using system call.

//ducskecode

#include<iostream>
using namespace std;
#include<fontl.h>
#include<stdlib.h>
#include<unistd.h>
void filecopy(int fi, int f2);
int main(int argc, char *argv[])
{
if(argc!=3)
{
cout<<"File not found ";
exit(1);
}
int fd1=open(argv[1], 0);
if(fd1==-1)
{
cout<<"\nError in file opening.";
exit(1);
}
int fd2=creat(argv[2], 0666);
if(fd2==-1)
{
cout<<"\n Error while creating file ";
exit(1);
}
filecopy (fdi, fd2);
close(fd1);
close(fd2);
return 0;
}
void filecopy (int fi, int f2)
{
char buf[512];
int cnt;
while(cnt=read(f1, buf, sizeof(buf)))
{
write(f2, buf, cnt) ;
}
}

Output:



4.A PROGRAM to print file details including owner access permissions, file access time, where file name is given as argument.

A PROGRAM to print file details including owner access permissions, file access time, where file name is given as argument.

//ducskecode

#include<iostream>
using namespace std;
#include<stdlib.h>
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
int i;
struct stat s;
if (argc < 2)
{
cout<<"\n enter filename in";
//exit();
}
for(i=1;i<argc; i++)
{
cout<<"File : "<<argv[i]<<"\n";
if(stat(argv[i],&s)<O)
cout<<"error in obtaining stats In":
else
{
cout<<"owner UID : "; cout<<s.st_uid; cout<<"\n";
cout<<"group ID :"; cout<<s.st_gid; cout<<"\n";
cout<<"Acess permissions : "; cout<<s.st_mode; COut<<"\n";
cout<<"Acess Time : :cout<<s.st_atime; cout<<"\n":
cout<<"File Size : "; cout<<s.st_size; cout<<"\n";
cout<<"File Size(in blocks) : "; cout<<s.st_blksize; cout<<"\n";
}
}
return 0;
}


Output:



2.A PROGRAM to report behaviour of Linux kernel including kernel version, CPU type and model. (CPU information)

2.A PROGRAM to report behaviour of Linux kernel including kernel version, CPU type and model. (CPU information)

OR

3.A PROGRAM to report behaviour of Linux kernel including information on configured memory, amount of free and used memory. (memory information)


//ducskecode

//ducskecode.blogspot.com
#include<iostream>
using namespace std
#include<stdlib.h>
#include<stdio.h>
int main() 
{
cout<<"\n The kernel version is, \n";
system("car/proc/sys/kernel/osrelease") ;
cout<<"\n The cpu space: \n";
system("cat /proc/cputnfo | awk 'NR==3, NR==4{print}' \n ");
cout<<"\n Amount of cpu time since system was last booted is: ";
system("cat /proc/uptime \n");
system("cat /proc/meminfo | awk 'NR==1, NR==4{print $2}' \n ");
cout<<"\n Amount of free memory: \n";
system("cat /proc/merminfo  |awk 'NR = 2{Print $2}' \n ")
cout<<"\n Amount of used memory is: \n";
system("cat /proc/meninfo | awk '{ if (NR==1) a=$2; if(NR==2) b=$2 }END {print a-b} ' \n");
cout<<endl;
return(0) ;
}

Output:




1. write a program (using fork() and/or exec() commands) where parent and child execute

/*WRITE A PROGRAM (using fork() and/or exec() commands) where parent and child execute:*/

a) same program, same code.

//ducskecode.blogspot.com
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main ()
{
int pid_t,pid, p;
p=fork();.
pid=getpid() ;
if(p<0)
{
printf(" Fork Failed ") ;
return 1;
}
printf("output of fork id %d \n",P);
printf("process id is : %d \n",pid);
return 0;
}

Output:




b) same program, different code

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int p_id;
pid fork();
if(p_id<0)
{
Print("\n Error "):
exit(1);
}
else if(p_id==0)
{
printf("\n Hello I an child process ");
printf("\n my pid is %d ",getpid ());
exit(o);
}
else
{
printf("\n Hello i am parent process ");
printf("\n My actual pid is %d \n",getpid ());
exit(1);
}
}

Output:


C) parents waits for child to terminate first then parent process terminates after child. 

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int pid;
pid=fork()
if(pid<0)
{
printf("\n Error ")
exit(1)
}
else if(pid==0)
{
printf("\n Hello I arn child process ");
printf("\n my pid is xd ",getpid ( ));
exit(0);
}
else if(pid>0)
{
Printf("\n Hello 1 an parent Process ").
printf("\n My actual pid is %d \n",getpid ());
wait(null);
exit(1);
}
}
Output:


Convert a binary number to ASCII character string.

8).Convert a binary number to ASCII character string.

.MODEL SMALL
.DATA
INPUT DB 10,13 , 'ENTER BINARY NO: $'
OUTPUT DB 10,13, 'THE ASCII CHARACTER IS:$'
ARR DB ?
.CODE
.STARTUP
MOV AH,09H
MOV DX,OFFSET INPUT
INT 21H
MOV BL, 00H
MOV CL,08H
INPUT1: MOV AH,01H
INT 21H
SUB AL,30H
SHL BL,1
ADD BL,AL
LOOP INPUT1
MOV AH,09H
LEA DX,OUTPUT
INT 21H
MOV AH,02H
MOV DL,BL
INT 21H
.EXIT

END

Convert an ASCII coded decimal number into its binary equivalent.

7).Convert an ASCII coded decimal number into its binary equivalent.

.MODEL SMALL
.DATA
MESG DB 10,13, 'ENTER NO: $'
RESULT DB 10,13, 'RESULT IS: $'
.CODE
.STARTUP
MOV DX,OFFSET MESG
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
MOV BL,AL
MOV DX,OFFSET RESULT
MOV AH,09H
0INT 21H
MOV CL,08H
MOV AH,00H
MOV AL,BL
L1: SHL AL, 01H
MOV BL,AL
MOV AL,00H
ADC AL,30H
MOV DL,AL
MOV AH,02H
INT 21H
MOV AL,BL
LOOP L1
.EXIT

END

WAP to add and subtract two arrays.

6).WAP to add and subtract two arrays.

Addition:

.model small
.data
mat1 db 12h, 11h, 12h, 10h, 11h, 12h, 10h, 11h, 12h
mat2 db 13h, 02h, 02h, 02h, 02h, 02h, 02h, 02h, 02h
res3 dw 9 dup(?)
.code                        
     mov   ax, @data      
     mov   ds, ax
     mov   cx, 09h      
     mov   di, offset mat1    
     mov   bx, offset mat2      
     mov   si, offset res3    
back :   mov   ah, 0        
     mov   al, [di]      
     sub   al, [bx]      
     adc   ah, 00        
     mov   [si], ax      
     inc   di        
     inc   bx        
     inc   si        
     inc   si  
     loop   back        
     mov   si, offset res3
     mov   dh, 9  
l10:    mov   ch, 04h      
     mov   cl, 04h      
     mov   bx, [si]      
l2:    rol   bx, cl        
     mov   dl, bl        
     and   dl, 0fh        
     cmp   dl, 09        
     jbe   l4
     add   dl, 07        
l4:    add   dl, 30h
     mov   ah, 02        
     int   21h  
     dec   ch        
     jnz   l2
     mov   dl, ' '    ;ye space h    
     int   21h  
     inc   si        
     inc   si  
     dec   dh        
     jnz   l10  
     mov   ah, 4ch      
     int   21h
   end

Subtraction:

.model small
.data
mat1 db 12h, 11h, 12h, 10h, 11h, 12h, 10h, 11h, 12h
mat2 db 13h, 02h, 02h, 02h, 02h, 02h, 02h, 02h, 02h
res3 dw 9 dup(?)
.code                        
     mov   ax, @data      
     mov   ds, ax
     mov   cx, 09h      
     mov   di, offset mat1    
     mov   bx, offset mat2      
     mov   si, offset res3    
back :   mov   ah, 0        
     mov   al, [di]      
     add   al, [bx]      
     adc   ah, 00        
     mov   [si], ax      
     inc   di        
     inc   bx        
     inc   si        
     inc   si  
     loop   back        
     mov   si, offset res3
     mov   dh, 9  
l10:    mov   ch, 04h      
     mov   cl, 04h      
     mov   bx, [si]      
l2:    rol   bx, cl        
     mov   dl, bl        
     and   dl, 0fh        
     cmp   dl, 09        
     jbe   l4
     add   dl, 07        
l4:    add   dl, 30h
     mov   ah, 02        
     int   21h  
     dec   ch        
     jnz   l2
     mov   dl, ' '    ;ye space h    
     int   21h  
     inc   si        
     inc   si  
     dec   dh       
     jnz   l10  
     mov   ah, 4ch      
     int   21h

   end

Write an assembly language program to sort a list.

5).Write an assembly language program to sort a list.

.model small
.386
.data
ARRAY DW 20 DUP (?)
DATA1 dw 0000H
msg db 10,13,"Enter the size of the array :: $"
msg2 db 10,13,"Enter the array :: $"
msg3 db 10,13,"The sorted array is :: $"
.code
.startup
MOV AH,09
MOV DX,OFFSET msg
INT 21H
MOV AH,01
INT 21H
SUB AL,30H
MOV AH,0
MOV CX,AX
MOV DATA1,AX
MOV AH,09
MOV DX,OFFSET msg2
INT 21H
MOV AH,0
MOV SI, 0
MOV BX, OFFSET ARRAY
L1: MOV DL, 0AH ; jump onto next line
MOV AH, 02H
INT 21H
MOV DX, SI ; input element of the array
MOV AH, 01H
INT 21H
SUB AL,30H
MOV SI, DX
MOV [BX + SI], AX
INC SI
LOOP L1
MOV CX, DATA1
MOV BX, OFFSET ARRAY
MOV DI,CX
L2: MOV CX, DATA1
MOV SI, 0
L3: MOV AL, [BX + SI]
CMP AL, [BX + SI + 1]
JL L4
XCHG AL,[BX + SI + 1]
MOV [BX + SI],AL
L4: INC SI
LOOP L3
DEC DI
JNZ L2
MOV CX, DATA1
MOV SI, 1
MOV BX, OFFSET ARRAY
MOV AH,09
MOV DX,OFFSET msg3
INT 21H
L5: MOV DL, 0AH ; jump onto next line
MOV AH, 02H
INT 21H
MOV DX, [BX + SI]
INC SI
ADD DL, 30H
MOV AH, 02
INT 21H
LOOP L5
.EXIT

END

Write an assembly language binary search, program to search a sorted list.

4).Write an assembly language binary search, program to search a sorted list.

.model small
.386
.data
ARRAY DW 20 DUP (?)
DATA1 dw 0000H
DATA2 dw 0000H
success db 10,13,"Element is present in the array $"
fail db 10,13,"Element is not present in the arary $"
msg db 10,13,"Enter the size of the array :: $"
msg2 db 10,13,"Enter the array :: $"
msg3 db 10,13,"Enter the element to be searched :: $"
.code
.startup
MOV AH,09
MOV DX,OFFSET msg
INT 21H
MOV AH,01
INT 21H
SUB AL,30H
MOV AH,0
MOV CX,AX
MOV DATA1,AX
MOV AH,09
MOV DX,OFFSET msg2
INT 21H
MOV AH,0
MOV SI, 0
MOV BX, OFFSET ARRAY
L1: MOV DL, 0AH ; jump onto next line
MOV AH, 02H
INT 21H
MOV DX, SI ; input element of the array
MOV AH, 01H
INT 21H
SUB AL,30H
MOV SI, DX
MOV [BX + SI], AX
INC SI
LOOP L1
MOV AH,09
MOV DX,OFFSET msg3
INT 21H
MOV AH,01 ; Enter element to be searched
INT 21H
SUB AL,30H
MOV DATA2,AX
MOV CX,DATA1
MOV SI,0
MOV DI, DATA1
MOV BP, 0
MOV BX, OFFSET ARRAY
MOV AX, DATA1
L2: MOV SI, DI
ADD SI, BP
MOV AX, SI
MOV DL, 2
DIV DL
MOV AH,0
MOV DX,0
MOV SI,AX
MOV DX,DATA2
CMP [BX + SI],DL
JZ L3
CALL L4
LOOP L2
MOV AH, 09H
MOV DX,OFFSET fail ; if the element is not found
INT 21H
MOV AH, 4CH ; to forcefully terminate the program
INT 21H
L3: MOV AH, 09H
MOV DX,OFFSET success ; if the element is found
INT 21H
MOV AH, 4CH
INT 21H
L4 PROC NEAR
CMP [BX+SI], DL
JL L6
MOV DI, SI
RET
L6: MOV BP,SI
RET
L4 ENDP
.EXIT
END

Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...