AMCAT Automata Questions
AMCAT Automata Questions
In Java
The same program written in Java is given below:
Output:
1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11
This is because, 32 + 42 = 52 .
There are two ways to solve this question.
1) The simple way is to generate triplets smaller than t given limit and check if it
meets the given condition every time. If the condition is true, print the triplets. Even
a = m 2 - n2 b = 2 * m * n
c = m2 + n2 because, a2
= m 4 + n 4 – 2 * m 2 * n 2 b2
= 4 * m2 * n2 c2 = m4 + n4
+ 2* m2 * n2
In C
The above method is written in C program as given below:
In Java
public class Amcat2 {
public static void main(String[] args) {
Scanner sc = new Scanner(in); int n
= sc.nextInt(); int[] arrivingTime =
new int[n]; int[] burstTime = new
int[n]; int timeQuant; int[] ans; for(int
i = 0; i < n; i++){
arrivingTime[i] = sc.nextInt();
}
for(int i = 0; i < n; i++) burstTime[i]
= sc.nextInt(); timeQuant =
sc.nextInt();
ans = waitingTime(n, arrivingTime, burstTime, timeQuant);
for(int i = 0; i < n; i++) out.print(ans[i] + " ");
out.println();
}
private static int[] waitingTime(int n, int[] a, int[] b, int q) {
int[] result = new int[n]; int[] processNo = new int[n];
for(int i = 0; i < n; i++) processNo[i] = i; b =
sort(a,b,processNo);
sort(a); int max =
b[0]; int index = 0;
for(int i = 1; i < n; i++)
if(max < b[i]){ max =
b[i];
index = i;
}
int[] c = new int[n]; int
time = 0; for(int i = 0; i <
n; i++){ c[i] = b[i];
out.println(a[i] + " " + b[i]);
}
//System.out.println("max : " + max);
while(b[index] != 0){ for(int i = 0; i <
n; i++){ if(b[i] == 0) continue;
if(b[i] >= q){
b[i] -= q; time
+= q; if(b[i]
== 0)
result[i] = time - a[i] - c[i];
}else{ time +=
q - b[i]; b[i] = 0;
In C++
JOIN PLACEMENT TALKS ON WHATSAPP (CLICK HERE)
JOIN PLACEMENT TALKS ON TELEGRAM (CLICK HERE)
#include<iostream>
#include<climits> using
namespace std;
int maxSubArraySum(int a[], int size)
{
int max_so_far = INT_MIN, max_ending_here = 0;
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here; if
(max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
/*Driver program to test maxSubArraySum*/ int
main()
{
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
int n = sizeof(a)/sizeof(a[0]); int max_sum =
maxSubArraySum(a, n); cout << “Maximum
contiguous sum is ” << max_sum; return 0;
}
In C:
#include <stdio.h> void
swap(int *, int *); void
bubble(int[], int); int
main()
{ int
n;
printf("Enter the size of n\n");
scanf("%d", &n); int
arr[n];
In Java
package arrayProject;
import java.util.*; import
java.math.*;
public class GCDPhrSe
{
public static int gcd(int n1,int n2)
{
if(n1==0) return n2;
if(n2==0) return n1;
if(n1>n2) return
gcd(n1-n2,n2); else
return gcd(n2-n1,n1);
}
public static void main(String args[])
{
Scanner sc=new Scanner(in); int
n1,n2;
System.out.println("Enter two no");
n1=sc.nextInt(); n2=sc.nextInt();
int n3=gcd(n1,n2);
System.out.println(n3);
}
}
In C Using Pointers
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#define TRUE 1 #
define FALSE 0 int
check_vowel(char);
In Java
public class SortedArray { public
static void main(String[] args) {
int a[]= {8,7,9}; int
b[]= {10,5};
sort(a,b);
}
public static void sort(int[] a,int[] b) {
int[] c= new int[a.length+b.length]; for
(int i = 0; i < a.length; i++) {
c[i]=a[i];
}
for (int i = 0, j=a.length; i < b.length; i++,j++) { c[j]=b[i];
}
for (int i = 0; i < c.length; i++) { for
(int j = i+1; j < c.length; j++) {
if(c[i]>c[j]) { int
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}}
out.print("Sorted Array : "); for
(int i = 0; i < c.length; i++) {
System.out.print(c[i]+" ");
}
}
}
1) Using + and -
2) Using * and /
#include<stdio.h> #include<stdlib.h>
intmain()
{
inta=10, b=20;
printf("Before swap a=%d b=%d",a,b); a=a*b;//a=200
(10*20)
b=a/b;//b=10 (200/20) a=a/b;//a=20
(200/10) system("cls");
printf("\nAfter swap a=%d b=%d",a,b); return0;
}
2) Divide the input integer by 10, obtain its remainder and quotient.
In C
/*C program to accept an integer & find the sum of its digits*/
#include <stdio.h>
void main()
{
long num, temp, digit, sum = 0;
printf("Enter the number \n");
scanf("%ld", &num); temp =
num;
while (num > 0)
{
digit = num % 10; sum
= sum + digit;
num /= 10;
}
printf("Given number = %ld\n", temp);
printf("Sum of the digits %ld = %ld\n", temp, sum);
}
10000000
Test Cases:
Case 1:
Input:[[1,1,1,][9,1,1],[0,1,0]] Expected
return value :1 Explanation:
The piece of cheese is placed at(1,0) on the grid
Mooshak can move from (0,0) to (1,0) to reach it or can
move from (0,0) to (0,1) to (1,1) to (1,0)
Test case 2:
Input:
[[0,0,0],[9,1,1],[0,1,1]] Expected
return value:0 Explanation:
Mooshak cannot move anywhere as there exists a wall
right on (0,0)
In C
Int isPath(int **grid,int m,int n)
{
if (x,y outside maze) return false if (x,y is goal) return
true if (x,y not open) return false mark x,y as part of
solution path if (FIND-PATH(North of x,y) == true)
return true if (FIND-PATH(East of x,y) == true) return
true if (FIND-PATH(South of x,y) == true) return true if
(FIND-PATH(West of x,y) == true) return true unmark
x,y as part of solution path return false*/ public
boolean findPath(int x, int y){ // check x,y are outside
maze. if(x < 0 || x >= mazelength || y < 0 || y >=
mazelength )
{
return false;
}
if(maze[x][y] == 9)
{
return true;
*head_ref = new_node;
while(current->next != *head_ref)
current->next = new_node;
new_node->next = *head_ref;
(d) change the head pointer to point to new node.
*head_ref = new_node;
< new_node->data)
{ current = current->next; }
In C
#include<stdio.h>
#include<stdlib.h> /*
structure for a node */
struct Node
{
Q1. There is a colony of 8 cells arranged in a straight line where each day every cell competes
with its adjacent cells (neighbour). Each day, for each cell, if its neighbours are both active and
both inactive, the cell becomes inactive the next day, otherwise it becomes active the next day.
Assumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can
be assumed to be always inactive.
Even after updating the cell state. Consider its previous state for updating the state of other cells.
Update the cell information of all cells simultaneously.
Write a function cell Compete which takes one 8 element array of integer’s cells representing the
current state of 8 cells and one integer days representing the number of days to simulate. An
integer value of 1 represents an active cell and value of 0 represents an inactive cell. Existing
Program
{/
Test Cases
TESTCASES 1:
INPUT:
[1,0,0,0,0,1,0,0],1
EXPECTED RETURN VALUE:
[0,1,0,0,1,0,1,0]
TESTCASE 2:
INPUT:
[1,1,1,0,1,1,1,1,],2
EXPECTED RETURN VALUE:
[0,0,0,0,0,1,1,0]
Solution:
#include<iostream> using
i=0;i<day;i++){ cells[-
1]=0; //assumptions
cells[8]=0;//assumptions
1;i<9;i++){ u[i]=cells[i];
to condition
else cells[j]=1;
}}
return 0;
Q2. Mooshak the mouse has been placed in a maze. There is a huge chunk of cheese
somewhere in the maze. The maze is represented as a two-dimensional array of integers, where
0 represents walls.1 represents paths where Mooshak can move and 9 represents the huge
chunk of cheese.
EX: 8 by 8(8*8) matrix maze where Mooshak can get the cheese.
10111001
10001111
10000000
10109011
11101001
10101101
10000101
11111111
Input:[[1,1,1,][9,1,1],[0,1,0]] Expected
return value :1 Explanation:
The piece of cheese is placed at(1,0) on the grid Mooshak can move from (0,0) to (1,0) to reach
it or can move from (0,0) to (0,1) to (1,1) to (1,0) Test case 2:
Input: [[0,0,0],[9,1,1],[0,1,1]] Expected
return value: 0 Explanation:
Mooshak cannot move anywhere as there exists a wall right on (0,0)
Existing Program
function is required*/
{/*
*/}
Solution:
if(maze[x][y] == 9) { return
true;
// move North
true;
//move West
maze[x][y] = 0; return
false;
j=0;j<mazelength;j++){
System.out.println();
ratMazeProblem.printSolution();
Q1. The least recently used (LRU) cache algorithm exists the element from the
cache(when it’s full) that was least recently used. After an element is requested from
the cache, it should be added to the cache (if not already there) and considered the
most recently used element 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 have pages numbered from 1 to 50.
TEST CASES:
TEST CASE1:
INPUT:
3,[7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0],16
2:
INPUT:
2,[2,3,1,3,2,1,4,3,2],9
EXPLANATION:
{/
}
Solved Program
(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++){
k<max_cache_size;k++){
/*if the value is already present in the cache then shifting values from the present
value.*/ cache[i]=cache[i+1];
/*if the value is not present in the cache then shifting values from starting.*/
cache[l]=cache[l+1];
cache[max_cache_size-1]=pages[i];
miss++;
} }}
*cellsptr=pages;
=t"<<lruCountMiss(3,cellsptr,16);//passing to function
Write a function to insert an integer into a circular linked _list whose elements are
sorted in ascending order 9smallest to largest). The input to the function insert
JOIN PLACEMENT TALKS ON WHATSAPP (CLICK HERE)
JOIN PLACEMENT TALKS ON TELEGRAM (CLICK HERE)
Sorted List is a pointer start to some node in the circular list and an integer n
between 0 and 100. Return a pointer to the newly inserted node...
Int value;
Cnode* next;
};C node* insertSortedList (cnode*
start,int n
{/
/WRITE YOUR CODE HERE
}/
/FUNCTION SIGNATURE ENDS
Test Case 1:
Input:
[3>4>6>1>2>^],5 Expected
Return Value:
[5>6>1>2>3>4>^]
Test Case 2:
Input:
[1>2>3>4>5>^],0 Expected
Return Value:
[0>1>2>3>4>5>^]
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.