AMCAT Coding Questions and Answers
AMCAT Coding Questions with Solutions
In this page, you will find out AMCAT Coding Questions and Answers asked in Online Assessments and Technical Assessments of the AMCAT Exam conducted for Hiring in Several Companies. Generally, AMCAT asks only Aptitude Questions but for Technical Job Profiles in AMCAT Parterned Company, they ask Coding Questions also.
Go through this page to get all Sample AMCAT Coding Questions included in AMCAT Online Technical Assessment.

About AMCAT Employability Test
- AMCAT is a computer adaptive test measuring job applicants in various areas: Communication, Logical reasoning, Quantitative skills, and Domain expertise.
- Unlike typical aptitude tests, AMCAT evaluates personality traits and domain-specific skills, enhancing job-candidate alignment.
- After the test, AMCAT suggests suitable jobs based on performance, and its Feedback report and Score Card highlight strengths and areas for improvement.
- AMCAT is becoming an interesting opportunity for getting entry-level roles in 700+ companies.
- Partnerships with industry leaders like Accenture, NTT Data, Mphasis, Cognizant, Wipro, Deloitte, ITC, JusPay, etc., across IT, Banking, Automobiles, and Telecom.
- At last, AMCAT is a Recruitment Exam that is the Best way to get multiple job opportunities in multiple companies by giving only its one Online Exam.
Steps involved in Hiring Through AMCAT
- Online Aptitude + Coding Test
- Performance Evaluation
- Technical & HR Interview
After reviewing your AMCAT Performance Report or Score Card, multiple companies will begin Scheduling technical and HR interviews.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
AMCAT Coding Questions With Solutions
Question 1: Fountain Installation
Fountains are installed at every position along a one-dimensional garden of length n. Array locations[] represents the coverage limit of these fountains. The ith fountain (where 1sisn) has a coverage limit of locations[i] that can range from the position max((i – locations[i]), 1) to min((i + locations[i]), n ). In other words, the h fountains do not reach outside the boundaries of the garden. In the beginning,all the fountains are switched off. Determine the minimum number of fountains that need to be activated to cover the n length garden by water.
Example
- n = 3
- locations[] = {0, 2, 13, then
- For position 1: locations[1] = 0, max((1 – 0),
- 1) to mini (1+0), 3) gives range = 1 to 1
- For position 2: locations[2] = 2, max((2-2),
- 1) to min( (2+2), 3) gives range = 1 to 3
- For position 3: locations[3] = 1, max( (3-1),
- 1) to min( (3+1), 3) gives range = 2 to 3
- For position 1: locations[1] = 0, max((1 – 0),
For the entire length of this garden to be covered, only the fountain at position 2 needs to be activated.
Function Description
Complete the function fountainActivation in the editor below.
fountainActivation has the following Parameter:
- int locations[n]: the fountain locations
Returns
- int: the minimum number of fountains that must be activated
Constraints
- 1<_n<_ 10^5
- O<_locations[i] <_ mini (n,100) (where 1 <_1<_10^5)
► Input Format For Custom Testing
Sample Case 0
Sample Input For Custom Testing
- 3 ->locations[] size n = 3
- 1 ->locations[] [1, 1, 1
- 1 ->Sample Output
Sample Output
- 1
Explanation
Here, locations = {1, 1, 13
- For position 1: locations[1] = 1, maxi (1 -1), 1) to min((1+1), 3) gives range = 1 to 2
- For position 2: locations[2] = 1, max( (2 -1), 1) to min( (2+1), 3) gives range = 1 to 3
- For position 3: locations[3] = 1, max((3 -1), 1) to min((3+1), 3) gyes range = 2 to 3
If the 2nd fountain is active, the range from position 7 to 3 will be covered. The total number of fountains needed is 1.
#include<bits/stdc++.h> #define ll long long using namespace std; bool compare(pair < int, int > A, pair < int, int > B) { if (A.first = B.first) return A.second < B.second; return A.first < B.first; } int solve(int location[], int n) { pair < int, int > range[n]; for (int i = 0; i < n; i++) { int id = i + 1; range[i].first = max(1, id - location[i]); range[i].second = min(n, id + location[i]); } sort(range, range + n, compare); int i = 0; int ans = 0; while (i < n) { pair < int, int > p = range[i]; ans++; while (i + 1 < n && range[i].first == range[i + 1].first) { p.second = max(p.second, range[i + 1].second); i++; } //cout<<p.second<<" "<<i<<endl; while (i < n && range[i].second <= p.second) i++; //cout<<p.second<<" "<<i<<endl; } return ans; } int main() { int n; cin >> n; int location[n]; for (int i = 0; i < n; i++) cin >> location[i]; cout << solve(location, n) << endl; return 0; }
import java.util.*; class Main { static int minCntFoun(int a[], int N) { int[] dp = new int[N + 1]; Arrays.fill(dp, -1); // Mark the reachable indices for each fountain for (int i = 0; i < N; i++) { int left = Math.max(i - a[i], 0); int right = Math.min(i + a[i]+1, N); dp[left] = Math.max(dp[left], right); } int cntfount = 1; int idxRight = dp[0]; int idxNext = 0; // Traverse the reachable indices and activate fountains for (int i = 0; i < N; i++) { idxNext=Math.max(idxNext,dp[i]); if(i==idxRight){ cntfount++; idxRight = idxNext; } } return cntfount; } // Driver Code public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n = scan.nextInt(); int[] location=new int[n]; for(int i=0;i < n;i++){ location[i]=scan.nextInt(); } System.out.print(minCntFoun(location, n)); } }
Question 2 : Collecting Candies
Problem Description
Question:- Krishna loves candies a lot, so whenever he gets them, he stores them so that he can eat them later whenever he wants to.
He has recently received N boxes of candies each containing Ci candies where Ci represents the total number of candies in the ith box. Krishna wants to store them in a single box. The only constraint is that he can choose any two boxes and store their joint contents in an empty box only. Assume that there are an infinite number of empty boxes available.
At a time he can pick up any two boxes for transferring and if both the boxes contain X and Y number of candies respectively, then it takes him exactly X+Y seconds of time. As he is too eager to collect all of them he has approached you to tell him the minimum time in which all the candies can be collected.
Input Format:
- The first line of input is the number of test case T
- Each test case is comprised of two inputs
- The first input of a test case is the number of boxes N
- The second input is N integers delimited by whitespace denoting the number of candies in each box
Output Format: Print minimum time required, in seconds, for each of the test cases. Print each output on a new line.
Constraints:
- 1 < T < 10
- 1 < N< 10000
- 1 < [Candies in each box] < 100009
S. No. | Input | Output |
---|---|---|
1 | 1 4 1 2 3 4 | 19 |
2 | 1 5 1 2 3 4 5 | 34 |
Explanation for sample input-output 1:
4 boxes, each containing 1, 2, 3 and 4 candies respectively.Adding 1 + 2 in a new box takes 3 seconds.Adding 3 + 3 in a new box takes 6 seconds.Adding 4 + 6 in a new box takes 10 seconds.Hence total time taken is 19 seconds. There could be other combinations also, but overall time does not go below 19 seconds.
Explanation for sample input-output 2:
5 boxes, each containing 1, 2, 3, 4 and 5 candies respectively.Adding 1 + 2 in a new box takes 3 seconds.Adding 3 + 3 in a new box takes 6 seconds.Adding 4 + 6 in a new box takes 10 seconds.Adding 5 + 10 in a new box takes 15 seconds.Hence total time taken is 34 seconds. There could be other combinations also, but overall time does not go below 33 seconds.
#include<stdio.h> #include<string.h> int main() { int i,j; int num_box=0,k=0,sum=0,times=0,tst_case,temp=0; long c[10000],s[10000]; printf("Enter the number of test case:"); scanf("%d",&tst_case); printf("Enter the number of boxes:"); for(int l=0;l<tst_case;l++) { scanf("%d",&num_box); } printf("Enter the number of candies in each box:"); for(i=0;i<num_box;i++) { scanf("%ld",&c[i]); } for(i=0;i<num_box;i++) { for(j=i+1;j<num_box;j++) { if(c[i]>c[j]) { temp=c[i]; c[i]=c[j]; c[j]=temp; } } } sum=0; k=0; for(i=0;i<num_box;i++) { sum=sum+c[i]; s[k]=sum; k++; } times=0; printf("Minimum time requried:"); for(i=1;i<k;i++) times=times+s[i]; printf("%d\n",times); return 0; }
Output 1 4 1 2 3 4 19
#include<bits/stdc++.h> using namespace std; int main() { int i,j; int num_box=0,k=0,sum=0,times=0,tst_case,temp=0; long c[10000],s[10000]; cout<<("Enter the number of test case:"); cin>>tst_case; cout<<("Enter the number of boxes:"); for(int l=0;l<tst_case;l++) { cin>>num_box; } cout<<("Enter the number of candies in each box:"); for(i=0;i<num_box;i++) { cin>>c[i]; } for(i=0;i<num_box;i++) { for(j=i+1;j<num_box;j++) { if(c[i]>c[j]) { temp=c[i]; c[i]=c[j]; c[j]=temp; } } } sum=0; k=0; for(i=0;i<num_box;i++) { sum=sum+c[i]; s[k]=sum; k++; } times=0; cout<<("Minimum time requried:"); for(i=1;i<k;i++) times=times+s[i]; cout<<times; return 0; }
Output 1 4 1 2 3 4 19
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n, i, k = 0, sum = 0, s1 = 0, t, temp = 0, j; long c[] = new long[1000009]; long s[] = new long[100009]; System.out.println("Enter a no"); t = sc.nextInt(); for (int l = 0; l < t; l++) { n = sc.nextInt(); for (i = 0; i < n; i++) c[i] = sc.nextLong(); for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (c[i] > c[j]) { temp = (int) c[i]; c[i] = c[j]; c[j] = temp; } } } sum = 0; k = 0; for (i = 0; i < n; i++) { sum = (int) (sum + c[i]); s[k] = sum; k++; } s1 = 0; for (i = 1; i < k; i++) s1 = (int) (s1 + s[i]); System.out.println(s1); } } }
Output 1 4 1 2 3 4 19
T = int(input()) arr = [] arr1 = [] for i in range(0, T): N = int(input()) for i in range(0, N): arr.append(int(input())) arr.sort() count = arr[0] for i in range(1, len(arr)): count = count + arr[i] arr1.append(count) print(sum(arr1))
Output 1 4 1 2 3 4 19
Question 3: Class Monitor
Problem Statement :
After JEE Mains, some students got admission into an engineering college. Now there is a class consisting of such n students, and the HOD came to say it is time to select the class monitor. But He never gets all of them at one time. So he brought a register, every time he gets someone with less rank than the previous time he cut the name and wrote the name of the student and the rank.
For a given number of ranks he gets each time, you have to predict how many names are cut in the list.
Constraints:
Number of Visiting<=10^9
ranks <=10000
Input Format:
Number of Visiting N in their first line
N space separated ranks the HOD gets each time
Output Format:
Number of ranks cut in the list
Sample Input:
6
4 3 7 2 6 1
Sample Output:
3
#include <bits/stdc++.h> using namespace std; int main() { int n, p, m = INT_MAX, ans = 0; cin >> n; vector < int > a(n); for (int i = 0; i < n; i++) { cin >> p; if (p < m) { m = p; ans++; } } cout << ans - 1; }
import java.util.Scanner; public class Main { public static void main(String[] args) { int n, p, ans = 0, m = Integer.MAX_VALUE; Scanner sc = new Scanner(System.in); n = sc.nextInt(); for (int i = 0; i < n; i++) { p = sc.nextInt(); if (p < m) { m = p; ans++; } } System.out.print(ans - 1); } }
n = int(input()) a = list(map(int, input().split())) m = a[0] ans = 0 for i in range(1, n): if a[i] < m: m = a[i] ans += 1 print(ans)
Question 4 : Seating Arrangement in Exam Hall
Problem Statement :
Semester exams are going on for university students. Examiners noticed that a group of people are trying to cheat. They marked students of that group as ‘1’ and students of another group ( who are not cheating ) as ‘0’
We can reduce cheating by not allowing students from group 1 to sit together, means no two students from group 1 can sit together. Seatings are marked using above conditions. Your task is to give the seating placement of nth possibility Possibility order from 1 to 10 is given below
[1 10 100 101 1000 1001 1010 10000 10001 10010]
Sample input :
3 → number of test cases
4
6
9
Sample output :
101
1001
10001
Explanation :
4th possibility is 101
6th possibility is 1001
9th possibility is 10001
#include<stdlib.h> #include<stdio.h> #include<string.h> int main() { int t; scanf("%d", &t); int a[1000]; // Assuming a maximum of 1000 elements in 'a' int m = -1; int m1 = -1; for (int i = 0; i < t; i++) { scanf("%d", &m1); a[i] = m1; if (m1 > m) { m = m1; } } int k2 = 2; int n = m + 1; char k1[1000][50]; // Assuming a maximum of 1000 elements in 'a' and a maximum length of 50 characters memset(k1, 0, sizeof(k1)); k1[1][0] = '1'; int a1 = 1; while (k2 < n) { if (k1[a1][strlen(k1[a1]) - 1] == '0') { strcpy(k1[k2], k1[a1]); strcat(k1[k2], "0"); k2++; if (k2 >= n) { break; } strcpy(k1[k2], k1[a1]); strcat(k1[k2], "1"); k2++; if (k2 >= n) { break; } a1++; } else if (k1[a1][strlen(k1[a1]) - 1] == '1') { strcpy(k1[k2], k1[a1]); strcat(k1[k2], "0"); k2++; if (k2 >= n) { break; } a1++; } } for (int i = 0; i < t; i++) { printf("%s\n", k1[a[i]]); } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n, m, Max = 0; cin >> n; vector < int > v(n); vector < string > arr; for (int i = 0; i < n; i++) { cin >> v[i]; Max = max(Max, v[i]); } queue < string > q; q.push("1"); int i = 1; arr.push_back("1"); while (!q.empty()) { cout<<"TEST"<<endl; string a = q.front(); q.pop(); q.push(a + "0"); arr.push_back(a + "0"); i++; if (a[a.length() - 1] == '0') { q.push(a + "1"); arr.push_back(a + "1"); i++; } if (i > Max) break; } for (int i = 0; i < n; i++) { cout << arr[v[i] - 1] << endl; } }
import java.util.*; class Main { public static void possibilities(int n) { int c = 0; String b = ""; for (int i = 1; n != c; i++) { String s = Integer.toString(i, 2); if (!s.contains("11")) { c++; b = s; } } System.out.println(b); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); int[] a = new int[tc]; for (int i = 0; i < tc; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < tc; i++) { possibilities(a[i]); } } }
t = int(input()) a = [] m = -1 m1 = -1 for i in range(t): m1 = int(input()) a.append(m1) m = max(m, m1) k2 = 2 n = m + 1 k1 = ["0"] * (n) k1[1] = "1" a1 = 1 while k2 < (n): if k1[a1][-1] == "0": k1[k2] = k1[a1] + "0" k2 += 1 if k2 >= (n): break k1[k2] = k1[a1] + "1" k2 += 1 if k2 >= (n): break a1 += 1 elif k1[a1][-1] == "1": k1[k2] = k1[a1] + "0" k2 += 1 if k2 >= (n): break a1 += 1 for i in a: print(k1[i])
Question 5: Formatting large Products
Problem Statement: Rohan is weak in mathematics.He is giving mathematics Olympiad , but he got stuck in one of the question .Help rohan to solve the question.In Question there are two positive integer A and B. You have to find the product of all integer between A and B which is represented in the form C=D*10^E , where C is the product of numbers , D and E are non-negative integers and the last digit of D is non-zero.
Function Description
- Complete the function formatProducts in the editor below, formatProduct must return a string that represents C in the above described form.
- Function has the following parameters
- A: an integer
- B: an integer
Constraints
- A will between 1 and 1,000,000 . Inclusive.
- B will be between A and 1,000,000. Inclusive.
Sample Input 0
- 1
- 5
Sample Output 0
- 12 * 10^1
Explanation
- 1*2*3*4*5=120 = 12 * 10^1
Sample Input 1
- 3
- 10
Sample Output 1
- 18144 * 10^2
Explanation
- 3*4*….*10=1814400 =18144 * 10^2
#include<bits/stdc++.h> using namespace std; string formatProducts(int a, int b) { int result = 1; for (int i = a; i <= b; i++) result = result * i; int temp = result; int power = 0; while ((result % 10) == 0) { power = power + 1; result = result / 10; } return to_string(result) + " * 10^" + to_string(power); } int main() { int a, b; cin >> a >> b; cout << formatProducts(a, b) << endl; return 0; }
import java.util.*; public class Main { public static String formatProducts (int a, int b) { int result = 1; for (int i = a; i <= b; i++) result = result * i; int temp = result; int power = 0; while ((result % 10) == 0) { power = power + 1; result = result / 10; } return result + " * 10^" + power; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int a = sc.nextInt (); int b = sc.nextInt (); System.out.println (formatProducts (a, b)); } }
def formatProducts(L,H): a=1 for i in range(L,H+1): a=a*i c=0 while(True): if a%10!=0: break else: c+=1 a=a//10 return (str(a)+" * 10^"+str(c)) L=int(input()) H=int(input()) print(formatProducts(L,H))
Question 6: Left Rotation
Problem Statement
A left rotation operation on an array shifts each of the array’s elements unit to the left. For example, if 2 left rotations are performed on array [1, 2, 3, 4, 5], then the array would become [3, 4, 5, 1, 2].
Given an array of integers and a number, , perform left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.
Function Description
Complete the function rotLeft in the editor below. It should return the resulting array of integers.
rotLeft has the following parameter(s):
- An array of integers .
- An integer , the number of rotations.
Input Format
The first line contains two space-separated integers and , the size of and the number of left rotations you must perform.
The second line contains space-separated integers a[i].
Constraints
- 1 <= n <= 10^5
- 1 <= d <= n
- 1 <= a[i] <= 10^8
Output Format
Print a single line of space-separated integers denoting the final state of the array after performing d left rotations.
Sample Input
5 4
1 2 3 4 5
Sample Output
5 1 2 3 4
Explanation
When we perform d=4 left rotations, the array undergoes the following sequence of changes:
[1,2,3,4,5] → [2,3,4,5,1] → [3,4,5,1,2] → [4,5,1,2,3] → [5,1,2,3,4]
Test Case : 1
Input (stdin)
- 5 4
- 1 2 3 4 5
Expected Output
- 5 1 2 3 4
Test Case : 2
Input (stdin)
- 20 10
- 41 73 89 7 10 1 59 58 84 77 77 97 58 1 86 58 26 10 86 51
Expected Output
- 77 97 58 1 86 58 26 10 86 51 41 73 89 7 10 1 59 58 84 77
#include int rotLeft(int arr[], int n, int d) { int i, j; int first; for(i=0; i<d; i++) { first = arr[0]; for(j=0; j<n-1; j++) { arr[j] = arr[j+1]; } arr[j] = first; } return *arr; } int main() { int n, d, i; scanf("%d",&n); scanf("%d",&d); int list[n]; for(i=0; i<n; i++) { scanf("%d",&list[i]); } rotLeft(list, n, d); for(i=0; i<n; i++) { printf("%d ",list[i]); } }
#include<bits/stdc++.h> using namespace std; int rotLeft(int arr[], int n, int d) { int i, j; int first; for(i=0; i<d; i++) { first = arr[0]; for(j=0; j<n-1; j++) { arr[j] = arr[j+1]; } arr[j] = first; } return *arr; } int main() { int n, d, i; cin>>n; cin>>d; int list[n]; for(i=0; i<n; i++) { cin>>list[i]; } rotLeft(list, n, d); for(i=0; i<n; i++) { cout<<list[i]<<" "; } }
import java.util.*; class Main { public static void rotateLeft(int a[],int n, int d) { int first,i,j; for(i=0;i<d;i++) { first=a[0]; for(j=0;j<n-1;j++) a[j]=a[j+1]; a[j]=first; } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int d=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); rotateLeft(a,n,d); for(int i=0;i<n;i++) System.out.print(a[i]+" "); } }
def rotateLeft(n,d,arr): for i in range(d): arr = rotatearr(n,arr) return arr def rotatearr(n, arr): first = arr[0] for i in range(n-1): arr[i] = arr[i+1] arr[n-1] = first return arr , d = map(int, input().split()) arr = list(map(int, input().split())) for i in rotateLeft(n,d,arr): print(i, end=" ")
Question 7: Trapezium pattern
Anirudh is attending an astronomy lecture. His professor who is very strict asks students to
Write a program to print the trapezium pattern using stars and dots as shown below . Since Anirudh is not good at astronomy can you help him?
Sample Input:
- N = 3
Output:
**.**
*…*
…..
*…*
**.**
#include<stdio.h> #include<string.h> int main(){ int i,j,n; scanf("%d",&n); for(i=0;i<n;i++){ for(j=0;j<n;j++){ if(j<n-i-1) printf("*"); else printf("."); } for(j=0;j<n-1;j++){ if(j<i) printf("."); else printf("*"); } printf("\n"); } for(i=2;i<=n;i++){ for(j=0;j<n;j++){ if(j<i-1) printf("*"); else printf("."); } for(j=0;j<n-1;j++){ if(j<n-i) printf("."); else printf("*"); } printf("\n"); } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int i,j; for(i=0;i<n;i++){ for(j=0;j<n;j++){ if(j<n-i-1) System.out.print("*"); else System.out.print("."); } for(j=0;j<n-1;j++){ if(j<i) System.out.print("."); else System.out.print("*"); } System.out.println(); } for(i=2;i<=n;i++){ for(j=0;j<n;j++){ if(j<i-1) System.out.print("*"); else System.out.print("."); } for(j=0;j<n-1;j++){ if(j<n-i) System.out.print("."); else System.out.print("*"); } System.out.println(); } } }
n=int(input()) for i in range(n): print("*"*(n-1-i)+"."*(2*i+1)+"*"*(n-1-i)) for i in range(n-1): print("*"*(i+1)+"."*(2*(n-2-i)+1)+"*"*(i+1))
Question 8: Vowel Encryption
Problem Statement :
There is an encryption game going on. You will be given a number. If a digit is prime, it will take a vowel. Otherwise it will take a consonant value.
By this process, you have to make the string the lexicographically smallest possible. For a given number, print the output as a string.;
Input Format:
An integer n denoting the number.
Output Format:
The encrypted word.
Sample Input: 123421
Sample Output: baecab
#include<bits/stdc++.h> using namespace std; map < char, char > M; map < int, bool > prime; int main() { prime[2] = true; prime[3] = true; prime[5] = true; prime[7] = true; char vow[] = { 'a', 'e', 'i', 'o', 'u' }; char con[] = { 'b', 'c', 'd', 'f', 'g', 'h' }; int jv = 0, jc = 0; string s; cin >> s; for (auto i: s) { if (prime[i - '0']) { if (M[i]) {} else { M[i] = vow[jv]; jv++; } } else if (M[i]) {} else { M[i] = con[jc]; jc++; } } for (auto i: s) cout << M[i]; }
import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String vowel = "aeiou"; char arr[] = new char[10]; arr[2] = 'a'; arr[3] = 'e'; arr[5] = 'i'; arr[7] = 'o'; char ch = 'b'; for (int i = 1; i < arr.length; i++) { if (vowel.indexOf(ch) != -1) { ch++; continue; } else if (arr[i] == 0) arr[i] = (char) ch++; } int temp = n; int res = 0; while (temp != 0) { res = res * 10 + temp % 10; temp = temp / 10; } int i = 1; while (res != 0) { System.out.print(arr[res % 10]); res = res / 10; } } }
s = input() res = [] x, y = 0, 0 v = "aeiou" c = "bcdfgh" p = "2357" ans = sorted(set(s)) for i in ans: if i in p: res.append(v[x]) x += 1 else: res.append(c[y]) y += 1 for i in s: print(res[ans.index(i)], end="")
Question 9: Array Sub Array
Problem Statement :
You are given an array, You have to choose a contiguous subarray of length ‘k’, and find the minimum of that segment, return the maximum of those minimums.
Sample input 0 :
1 → Length of segment x =1
5 → size of space n = 5
1 → space = [ 1,2,3,1,2]
2
3
1
2
Sample output :
3
Explanation :
The subarrays of size x = 1 are [1],[2],[3],[1], and [2],Because each subarray only contains 1 element, each value is minimal with respect to the subarray it is in. The maximum of these values is 3. Therefore, the answer is 3
#include <bits/stdc++.h> using namespace std; vector < int > arr; int prevmin = -1; int flag = 0; int x, n, q; int sorting(int start, int end) { if (start + 1 == n) { start = 0; end = end - n; } if (start == end) return arr[start]; return min(arr[start], sorting(start + 1, end)); } int func(int start, int end) { if (flag == 0) { flag++; return prevmin = sorting(start, end); } if (arr[start - 1] == prevmin) return prevmin; return prevmin = (arr[end] <= prevmin) ? prevmin : sorting(start, end); } int main() { cin >> x >> n; int ans = 0; for (int i = 0; i < n; i++) { cin >> q; arr.push_back(q); } for (int i = 0; i < n; i++) { ans = max(ans, func(i, i + x - 1)); } cout << ans; }
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int n = sc.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int i = 0; i <= n - x; i++) { min = Integer.MAX_VALUE; for (int j = i; j < (i + x); j++) min = Math.min(min, arr[j]); max = Math.max(min, max); } System.out.println(max); } }
s = int(input()) n = int(input()) a = [] for i in range(n): a.append(int(input())) def min_in_segment(start, end, prev, s, prev_min): if s == 1: return a[start] else: if prev == -1 or prev_min == -2: return min(a[start : end + 1]) elif prev_min != -2: if prev != prev_min: if a[end] < prev_min: return a[end] else: return prev_min else: return min(a[start : end + 1]) msf = -1 prev = -1 prev_min = -2 for i in range(n - s + 1): new_min = min_in_segment(i, i + s - 1, prev, s, prev_min) msf = max(msf, new_min) prev = a[i] prev_min = new_min print(msf)
Question 10: Queries for Count
The task is to determine the number of elements within a specified range in an unsorted array. Given an array of size n, the goal is to count the elements that fall between two given values, i and j, inclusively.
Examples:
Input:
Array: [1, 3, 3, 9, 10, 4]
Range 1: i = 1, j = 4
Range 2: i = 9, j = 12
Output:
For Range 1: 4
For Range 2: 2
Explanation:
In the first query, the numbers within the range 1 to 4 are 1, 3, 3, and 4.
In the second query, the numbers within the range 9 to 12 are 9 and 10.
#include<iostream> #include<algorithm> using namespace std; int findLowerIndex(int arr[], int n, int x) { int low = 0, high = n - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] >= x) high = mid - 1; else low = mid + 1; } return low; } int findUpperIndex(int arr[], int n, int y) { int low = 0, high = n - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] <= y) low = mid + 1; else high = mid - 1; } return high; } int countElementsInRange(int arr[], int n, int x, int y) { int count = 0; count = findUpperIndex(arr, n, y) - findLowerIndex(arr, n, x) + 1; return count; } int main() { int arr[] = {1, 4, 4, 9, 10, 3}; int n = sizeof(arr) / sizeof(arr[0]); sort(arr, arr + n); int lowerRange = 1, upperRange = 4; cout << countElementsInRange(arr, n, lowerRange, upperRange) << endl; lowerRange = 9; upperRange = 12; cout << countElementsInRange(arr, n, lowerRange, upperRange) << endl; return 0; }
def find_lower_index(arr, x): low, high = 0, len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr[mid] >= x: high = mid - 1 else: low = mid + 1 return low def find_upper_index(arr, y): low, high = 0, len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr[mid] <= y: low = mid + 1 else: high = mid - 1 return high def count_elements_in_range(arr, x, y): arr.sort() lower_index = find_lower_index(arr, x) upper_index = find_upper_index(arr, y) count = upper_index - lower_index + 1 return count arr = [1, 4, 4, 9, 10, 3] lower_range = 1 upper_range = 4 print(count_elements_in_range(arr, lower_range, upper_range)) lower_range = 9 upper_range = 12 print(count_elements_in_range(arr, lower_range, upper_range))
import java.util.Arrays; class Main { static int findLowerIndex(int[] arr, int n, int x) { int low = 0, high = n - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] >= x) high = mid - 1; else low = mid + 1; } return low; } static int findUpperIndex(int[] arr, int n, int y) { int low = 0, high = n - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] <= y) low = mid + 1; else high = mid - 1; } return high; } static int countElementsInRange(int[] arr, int n, int x, int y) { int count = 0; count = findUpperIndex(arr, n, y) - findLowerIndex(arr, n, x) + 1; return count; } public static void main(String[] args) { int[] arr = {1, 4, 4, 9, 10, 3}; int n = arr.length; Arrays.sort(arr); int lowerRange = 1, upperRange = 4; System.out.println(countElementsInRange(arr, n, lowerRange, upperRange)); lowerRange = 9; upperRange = 12; System.out.println(countElementsInRange(arr, n, lowerRange, upperRange)); } }
FAQs related to AMCAT Coding Questions
Question 1: What kind of questions are asked in AMCAT?
In AMCAT Exam Quants, Logical Reasoning, Verbal Ability and Domain Specific Question are included.
Question 2: What is minimum score in AMCAT exam?
There is no such minimum criteria for Qualifying AMCAT Exam.
Question 3: Does AMCAT test have coding questions?
Yes, AMCAT Exam Includes Coding Question, if the applicant has opted for the same. Along with Aptitude Section.
Following mentioned topics are included in AMCAT Coding Test:
- Basic Programming
- Conditional statements
- Linear data structures
- Advanced data structures
- Sorting and searching algorithms
- Control Structures Based Coding
Question 4: What questions are asked in AMCAT Coding Test?
Following mentioned topics are included in AMCAT Coding Test:
- Basic Programming
- Conditional statements
- Linear data structures
- Advanced data structures
- Sorting and searching algorithms
- Control Structures Based Coding
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others