SlideShare a Scribd company logo
5
Most read
13
Most read
14
Most read
Virtusa content questions
1. Prefix-Sufix balance
You are given an array A of N integers. The array A can be divided
into two parts: the first part consists of the first "i" elements of A
(where ‘i’ range from 1 to N), and the second part consists of the
last (N-i) elements of A.
Your task is to find and return a new array named result of the same
Size as A, where each element of result[i] represents the absolute
difference between the sum of the elements in the first part of A
and the sum of the elements in the second part of A.
Note: For i = N, N-i = 0. So, consider the sum of last N-i integers as 0
in this case.
Input Specification:
Input1: An integer value N, representing the size of the array A.
input2: An integer array A.
Output Specification:
Return a new integer array named result of the same size as A,
where each element of result[i] represents the absolute difference
between the sum of the elements in the first part of A and the sum
of the elements in the second part of A
Example 1:
Input1 : 5
input2: {1,2,3,4,5}
Output: (13,9,3,5,15)
Explanation:
We have been given the value of N as 5, the array A is (1,2,3,4,5).
We can create the result array by the following way:
result[1] = absolute(1-(2+3+4+5)) = 13
result[2] = absolute((1+2)-(3+4+5)) = 9
result[3] = absolute((1+2+3)-(4+5)) = 3
result[4] = absolute((1+2+3+4)-(5)) =5
result[5] = absolute((1+2+3+4+5)-(0)) = 15
Hence, (13,9,3,5,15) is returned as the output.
Example 2:
input1:3
input2: {15,45,12}
Output: (42,48,72)
import java.util.*;
import java.io.*;
class Main {
public static void prefixSuffix(int arr[], int n){
int sum = 0, temp = 0;
for(int i = 0; i < n; i++)
sum += arr[i];
for(int i = 0; i < n; i++) {
temp += arr[i];
int t = sum - temp;
arr[i] = Math.abs(t - temp);
System.out.print(arr[i] + " ");
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextInt();
prefixSuffix(arr, n);
}
}
2. Minimum Array Sum
Paul is given an array A of length N. He must perform the
following operations on the array sequentially:
• Choose any two integers from the array and calculate their
average.
• If an element is less than the average, update it to 0.
However if the element is greater than or equal to the
average, he need not update it.
Your task is to help Paul find and return an integer value,
representing the minimum possible sum of all the elements in the
array by performing the above operations.
Note: An exact average should be calculated even if it results in a
decimal.
Input Specification:
input1: An integer value N, representing the size of the array A
input2: An integer array A
Output Specification:
Return an integer value, representing the minimum possible sum of
all the elements in the array by performing the above operations.
Example 1:
input1 : 5
input2: (1,4,2,9,5)
output:9
Explanation:
Here, we have been given the array (1,4,2,9,5).
• Paul selects 5 and 9 from this array.
• The average of these two numbers is (5+9)/2=7.
• Following the process, Paul updates each element in the array to 0,
except for 9, as it is greater than 7, and all other elements are less
than 7.
• Therefore, the updated array becomes (0, 0, 0, 9, 0).
• The sum of these updated elements is 0+0+0+9+0=9.
If Paul were to select any other pair of elements, the resulting sum 9.
Hence, 9 is returned.
Example 2:
input1 : 3
input2: (1,2,3)
Output: 3
import java.util.*;
import java.io.*;
class Main {
public static int minimumArray(int arr[], int n) {
int i, first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (i = 0; i < n; i++){
if (arr[i] > first) {
second = first;
first = arr[i];
}
else if (arr[i] > second && arr[i] != first)
second = arr[i];
}
int avg = (first + second) / 2;
int sum = 0;
for(i = 0; i < n; i++){
if(arr[i] <= avg)
arr[i] = 0;
sum += arr[i];
}
return sum;
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextInt();
System.out.println(minimumArray(arr, n));
}
}
3.String swap
Given a string S, you need to perform M operations on the string
based on an array A of length M, which contains the sequence of
operations to be performed. The operations are of two types:
1. Swapping the first and last characters of the string.
2. Swapping the first N/2 and the last N/2 characters of the string
where N is the length of the string.
Your task is to find and return the final string value that will be
obtained after performing all M operations in the given sequence.
Note:
• The array contains either 1 or 2 as values representing the operation
number.
• The length of string is always even.
• Assume 0 based indexing.
Input Specification:
Input1:A string S, representing the initial string
input2: An integer M, representing size of the array.
input3: An integer array A, where each element represents the
number of the operation that is to be performed.
output Specification:
Return the final string value that will be obtained after performing
all M operations in the given sequence.
Example 1:
input1: ABCD
input2: 3
input3: (1,2,1)
Output: BADC
Explanation:
Here in this example, on the string "ABCD" we can perform the
sequence of operations in the following way:
• The first operation is of type 1, we convert "ABCD" to "DBCA“
• The second operation is of type 2, we convert "DBCA" to
"CADB".ntanm
• The third operation is of type 1, we convert "CADB" to
"BADC"String.
Therefore, the final string obtained after performing all the
operations is "BADC". Hence, BADC is returned as the output.
Example 2:
input1 : MERCER
input2 : 3
input3: (1,1,2)
Output: CERMER
import java.util.*;
class Main {
public static String stringSwap(String str, int arr[], int n) {
char[] ch = str.toCharArray();
int len = str.length();
for(int i = 0; i < n; i++) {
if(arr[i] == 1) {
char temp = ch[0];
ch[0] = ch[len-1];
ch[len-1] = temp;
}
if(arr[i] == 2) {
int half = len/2;
for(int j = 0; j < half; j++) {
char temp = ch[j];
ch[j] = ch[j+half];
ch[j+half] = temp;
}
}
}
String s = new String(ch);
return s;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int n = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println(stringSwap(str, arr, n));
}
}
4. Magic String
Eva has a string S containing lowercase English letters. She wants to
transform this string into a Magic String, where all the characters in
the string are the same. To do so, she can replace any letter in the
string with another letter present in that string.
Your task is to help Eva find and return an integer value,
representing the minimum number of steps required to form a
Magic String. Return 0, if S is already a Magic String.
Input Specification:
input1: A string S containing lowercase English letters.
Output Specification:
Return an integer value, representing the minimum number of steps
required to form a Magic String. Return 0. if S is already a Magic
String
Example 1:
input1: aaabbbccdddd
Output: 8
Explanation:
Here in this example, the string S is aaabbbccdddd. We need perform
the following operations to make a Magic String:
1st way:
• On replacing the letters b, c d with a, the Magic string formed is
aaaaaaaaaaaa
• Here, the minimum number of steps to convert the original string
to Magic String is 9.
Optimal Way:
• On replacing the letters, a, b, c with d, the Magic string formed is
dddddddddddd.
• Here, the minimum number of steps required to form the Magic
String is 8.
Since 8 is smaller than 9. 8 is returned as the output.
Example 2:
Input 1: aaaa
Output: 0
import java.util.*;
class Main {
static final int SIZE = 26;
static void printCharWithFreq(String str) {
int n = str.length();
int[] freq = new int[SIZE];
for (int i = 0; i < n; i++) {
freq[str.charAt(i) - 'a']++;
}
int max = -1;
for (int i = 0; i < n; i++) {
if (freq[str.charAt(i) - 'a'] != 0) {
if(max < freq[str.charAt(i) - 'a']) {
max = freq[str.charAt(i) - 'a'];
freq[str.charAt(i) - 'a'] = 0;
}
}
}
System.out.println(n - max);
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
printCharWithFreq(str);
}
}

More Related Content

PPT
Shell sort
DOCX
QA Auotmation Java programs,theory
PPTX
First session _Cracking the coding interview.pptx
DOCX
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
PDF
Problem 2.19. How much time is required to add or subtract two large.pdf
DOCX
Maharishi University of Management (MSc Computer Science test questions)
PDF
Java Simple Programs
PDF
LeetCode April Coding Challenge
Shell sort
QA Auotmation Java programs,theory
First session _Cracking the coding interview.pptx
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Problem 2.19. How much time is required to add or subtract two large.pdf
Maharishi University of Management (MSc Computer Science test questions)
Java Simple Programs
LeetCode April Coding Challenge

Similar to Virtusa questions placement preparation guide (20)

PPTX
TCS_Digital_Advanced_Coding_Student copy.pptx
PDF
accenture Advanced coding questiosn for online assessment preparation
PDF
Microsoft word java
PDF
Dynamic programming
PDF
Computer Science Paper 1 (Theory) - 2017.pdf
PDF
OrderTest.javapublic class OrderTest {       Get an arra.pdf
PDF
Assignment 6 as a reference public class ArrExample pr.pdf
PPTX
MODULE-1(b) - Machine-Instructions-and-Programs.pptx
PPTX
MODULE-1(b) - Machine-Instructions-and-Programs.pptx
PPTX
MODULE-1(b) - Machine-Instructions-and-Programs.pptx
PDF
Computer java programs
PPT
2DArrays.ppt
PDF
Programming in Java: Arrays
PPTX
C PROGRAMS - SARASWATHI RAMALINGAM
PDF
Oot practical
PPTX
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
PPT
12650891 (1).ppthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
DOCX
Functions Practice Sheet.docx
PDF
Chapter 4
PDF
TCS_Digital_Advanced_Coding_Student copy.pptx
accenture Advanced coding questiosn for online assessment preparation
Microsoft word java
Dynamic programming
Computer Science Paper 1 (Theory) - 2017.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
Assignment 6 as a reference public class ArrExample pr.pdf
MODULE-1(b) - Machine-Instructions-and-Programs.pptx
MODULE-1(b) - Machine-Instructions-and-Programs.pptx
MODULE-1(b) - Machine-Instructions-and-Programs.pptx
Computer java programs
2DArrays.ppt
Programming in Java: Arrays
C PROGRAMS - SARASWATHI RAMALINGAM
Oot practical
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
12650891 (1).ppthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
Functions Practice Sheet.docx
Chapter 4
Ad

Recently uploaded (20)

PPT
notes_Lecture2 23l3j2 dfjl dfdlkj d 2.ppt
PPTX
Unit 1- Introduction to Corporate Etiquettes
DOCX
PRACTICE-TEST-12 is specially designed for those
PPTX
FINAL PPT.pptx cfyufuyfuyuy8ioyoiuvy ituyc utdfm v
PDF
Sheri Ann Lowe Compliance Strategist Resume
PPTX
Unit 2 CORPORATE CULTURE AND EXPECTATIONS
PPTX
D1basicstoloopscppforbeginnersgodoit.pptx
PPT
NO000387 (1).pptsbsnsnsnsnsnsnsmsnnsnsnsjsnnsnsnsnnsnnansnwjwnshshshs
PDF
202s5_Luciano André Deitos Koslowski.pdf
PPTX
GPAT Presentation PPT and details about imp topics.pptx
PDF
CV of Architect Professor A F M Mohiuddin Akhand.pdf
PPTX
_Dispute Resolution_July 2022.pptxmhhghhhh
PDF
Beyond the Lab Coat - Perjalanan Karier di Dunia Pasca-Fisika S1
PPTX
CYBER SECURITY PPT.pptx CYBER SECURITY APPLICATION AND USAGE
PPTX
employee on boarding for jobs for freshers try it
PPTX
ChandigarhUniversityinformationcareer.pptx
PPTX
A slide for students with the advantagea
PPTX
Slideham presentation for the students a
PPT
444174684-Welding-Presentatiohhhn-ppt.ppt
PDF
LSR CASEBOOK 2024-25.pdf. very nice casbook
notes_Lecture2 23l3j2 dfjl dfdlkj d 2.ppt
Unit 1- Introduction to Corporate Etiquettes
PRACTICE-TEST-12 is specially designed for those
FINAL PPT.pptx cfyufuyfuyuy8ioyoiuvy ituyc utdfm v
Sheri Ann Lowe Compliance Strategist Resume
Unit 2 CORPORATE CULTURE AND EXPECTATIONS
D1basicstoloopscppforbeginnersgodoit.pptx
NO000387 (1).pptsbsnsnsnsnsnsnsmsnnsnsnsjsnnsnsnsnnsnnansnwjwnshshshs
202s5_Luciano André Deitos Koslowski.pdf
GPAT Presentation PPT and details about imp topics.pptx
CV of Architect Professor A F M Mohiuddin Akhand.pdf
_Dispute Resolution_July 2022.pptxmhhghhhh
Beyond the Lab Coat - Perjalanan Karier di Dunia Pasca-Fisika S1
CYBER SECURITY PPT.pptx CYBER SECURITY APPLICATION AND USAGE
employee on boarding for jobs for freshers try it
ChandigarhUniversityinformationcareer.pptx
A slide for students with the advantagea
Slideham presentation for the students a
444174684-Welding-Presentatiohhhn-ppt.ppt
LSR CASEBOOK 2024-25.pdf. very nice casbook
Ad

Virtusa questions placement preparation guide

  • 2. 1. Prefix-Sufix balance You are given an array A of N integers. The array A can be divided into two parts: the first part consists of the first "i" elements of A (where ‘i’ range from 1 to N), and the second part consists of the last (N-i) elements of A. Your task is to find and return a new array named result of the same Size as A, where each element of result[i] represents the absolute difference between the sum of the elements in the first part of A and the sum of the elements in the second part of A. Note: For i = N, N-i = 0. So, consider the sum of last N-i integers as 0 in this case.
  • 3. Input Specification: Input1: An integer value N, representing the size of the array A. input2: An integer array A. Output Specification: Return a new integer array named result of the same size as A, where each element of result[i] represents the absolute difference between the sum of the elements in the first part of A and the sum of the elements in the second part of A Example 1: Input1 : 5 input2: {1,2,3,4,5} Output: (13,9,3,5,15)
  • 4. Explanation: We have been given the value of N as 5, the array A is (1,2,3,4,5). We can create the result array by the following way: result[1] = absolute(1-(2+3+4+5)) = 13 result[2] = absolute((1+2)-(3+4+5)) = 9 result[3] = absolute((1+2+3)-(4+5)) = 3 result[4] = absolute((1+2+3+4)-(5)) =5 result[5] = absolute((1+2+3+4+5)-(0)) = 15 Hence, (13,9,3,5,15) is returned as the output. Example 2: input1:3 input2: {15,45,12} Output: (42,48,72)
  • 5. import java.util.*; import java.io.*; class Main { public static void prefixSuffix(int arr[], int n){ int sum = 0, temp = 0; for(int i = 0; i < n; i++) sum += arr[i]; for(int i = 0; i < n; i++) { temp += arr[i]; int t = sum - temp; arr[i] = Math.abs(t - temp); System.out.print(arr[i] + " "); } }
  • 6. public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int arr[] = new int[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextInt(); prefixSuffix(arr, n); } }
  • 7. 2. Minimum Array Sum Paul is given an array A of length N. He must perform the following operations on the array sequentially: • Choose any two integers from the array and calculate their average. • If an element is less than the average, update it to 0. However if the element is greater than or equal to the average, he need not update it. Your task is to help Paul find and return an integer value, representing the minimum possible sum of all the elements in the array by performing the above operations.
  • 8. Note: An exact average should be calculated even if it results in a decimal. Input Specification: input1: An integer value N, representing the size of the array A input2: An integer array A Output Specification: Return an integer value, representing the minimum possible sum of all the elements in the array by performing the above operations. Example 1: input1 : 5 input2: (1,4,2,9,5) output:9
  • 9. Explanation: Here, we have been given the array (1,4,2,9,5). • Paul selects 5 and 9 from this array. • The average of these two numbers is (5+9)/2=7. • Following the process, Paul updates each element in the array to 0, except for 9, as it is greater than 7, and all other elements are less than 7. • Therefore, the updated array becomes (0, 0, 0, 9, 0). • The sum of these updated elements is 0+0+0+9+0=9. If Paul were to select any other pair of elements, the resulting sum 9. Hence, 9 is returned. Example 2: input1 : 3 input2: (1,2,3) Output: 3
  • 10. import java.util.*; import java.io.*; class Main { public static int minimumArray(int arr[], int n) { int i, first = Integer.MIN_VALUE, second = Integer.MIN_VALUE; for (i = 0; i < n; i++){ if (arr[i] > first) { second = first; first = arr[i]; } else if (arr[i] > second && arr[i] != first) second = arr[i]; } int avg = (first + second) / 2; int sum = 0;
  • 11. for(i = 0; i < n; i++){ if(arr[i] <= avg) arr[i] = 0; sum += arr[i]; } return sum; } public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int arr[] = new int[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextInt(); System.out.println(minimumArray(arr, n)); } }
  • 12. 3.String swap Given a string S, you need to perform M operations on the string based on an array A of length M, which contains the sequence of operations to be performed. The operations are of two types: 1. Swapping the first and last characters of the string. 2. Swapping the first N/2 and the last N/2 characters of the string where N is the length of the string. Your task is to find and return the final string value that will be obtained after performing all M operations in the given sequence. Note: • The array contains either 1 or 2 as values representing the operation number. • The length of string is always even. • Assume 0 based indexing.
  • 13. Input Specification: Input1:A string S, representing the initial string input2: An integer M, representing size of the array. input3: An integer array A, where each element represents the number of the operation that is to be performed. output Specification: Return the final string value that will be obtained after performing all M operations in the given sequence. Example 1: input1: ABCD input2: 3 input3: (1,2,1) Output: BADC
  • 14. Explanation: Here in this example, on the string "ABCD" we can perform the sequence of operations in the following way: • The first operation is of type 1, we convert "ABCD" to "DBCA“ • The second operation is of type 2, we convert "DBCA" to "CADB".ntanm • The third operation is of type 1, we convert "CADB" to "BADC"String. Therefore, the final string obtained after performing all the operations is "BADC". Hence, BADC is returned as the output. Example 2: input1 : MERCER input2 : 3 input3: (1,1,2) Output: CERMER
  • 15. import java.util.*; class Main { public static String stringSwap(String str, int arr[], int n) { char[] ch = str.toCharArray(); int len = str.length(); for(int i = 0; i < n; i++) { if(arr[i] == 1) { char temp = ch[0]; ch[0] = ch[len-1]; ch[len-1] = temp; } if(arr[i] == 2) { int half = len/2; for(int j = 0; j < half; j++) { char temp = ch[j]; ch[j] = ch[j+half]; ch[j+half] = temp; }
  • 16. } } String s = new String(ch); return s; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int n = sc.nextInt(); int arr[] = new int[n]; for(int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } System.out.println(stringSwap(str, arr, n)); } }
  • 17. 4. Magic String Eva has a string S containing lowercase English letters. She wants to transform this string into a Magic String, where all the characters in the string are the same. To do so, she can replace any letter in the string with another letter present in that string. Your task is to help Eva find and return an integer value, representing the minimum number of steps required to form a Magic String. Return 0, if S is already a Magic String. Input Specification: input1: A string S containing lowercase English letters. Output Specification: Return an integer value, representing the minimum number of steps required to form a Magic String. Return 0. if S is already a Magic String
  • 18. Example 1: input1: aaabbbccdddd Output: 8 Explanation: Here in this example, the string S is aaabbbccdddd. We need perform the following operations to make a Magic String: 1st way: • On replacing the letters b, c d with a, the Magic string formed is aaaaaaaaaaaa • Here, the minimum number of steps to convert the original string to Magic String is 9. Optimal Way: • On replacing the letters, a, b, c with d, the Magic string formed is dddddddddddd.
  • 19. • Here, the minimum number of steps required to form the Magic String is 8. Since 8 is smaller than 9. 8 is returned as the output. Example 2: Input 1: aaaa Output: 0
  • 20. import java.util.*; class Main { static final int SIZE = 26; static void printCharWithFreq(String str) { int n = str.length(); int[] freq = new int[SIZE]; for (int i = 0; i < n; i++) { freq[str.charAt(i) - 'a']++; } int max = -1; for (int i = 0; i < n; i++) { if (freq[str.charAt(i) - 'a'] != 0) { if(max < freq[str.charAt(i) - 'a']) { max = freq[str.charAt(i) - 'a']; freq[str.charAt(i) - 'a'] = 0; } } } System.out.println(n - max); }
  • 21. public static void main(String args[]) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); printCharWithFreq(str); } }