Virtusa Coding Questions
Virtusa Coding Questions
Code 1:
Problem Statement
Given a number N and another number K, find the last digit number formed when N is raised to the power K.
Note: The values of N and K can be very large.
Input Format:
First line of input contains the number N.
Second line of input contains the number K.
Output Format:
Return the last digit of the number formed when N is raised to the power of K.
Sample Input:
3
2
Sample Output:
9
Explanation:
3 raised to power 2 is 9 and the last digit is also 9.
Answer code
import java.util.*;
class Main
{
static int mbaChallenge(int n, int p)
{
int result = (int)Math.pow(n, p);
return result % 10;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int p = sc.nextInt();
System.out.print(mbaChallenge(n,p));
}
}
Code 2:
Problem Statement
Everyone has experienced a phase where it is too tough to find a best friend. Monica is goingthrough the same. Although she h as
got Rachel but sometimes she feels like she is still alone. Sheshares her concerns with Joey, who after listening the whole story,
agrees to be her best friend butunder one condition.
He asks her to make him a special dish everyday. A dish is special if it consists of only specialingredients. Monica has a lot of
ingredients, but she has 3 special ones which Joey likes a lot,represented by 1, 2 and 3. A dish is special if it consists of these 3
special ingredients in exactlyequal proportions. She also has to keep one th ing in mind i.e. she cannot change the order
oringredients, otherwise the dish would not be tasty.
She is curious in finding how many ways exist to make a special dish with the list of ingredients, sothat she makes Joey happ y.
Input Format:
First line contains a 1-indexed string array representing a list of ingredients in an order.
Second line contains the size of string.
Output Format::
Return the number of ways to make a special dish
Sample Input:
0123
4
Sample Output
1
Explanation
There is only 1 dish possible which can contain all three ingredients which would be (123), hence,the output i s 1.
Answer code
import java.util.*;
Scanner sc = new Scanner(System.in);
class Main
String s = sc.next();
{
int n = sc.nextInt();
static int fun(String s, int n)
System.out.print(fun(s, n));
{
}
int one = 0, two = 0, three = 0;
}
for(int i = 0; i < n; i++)
if(s.charAt(i) == '1')
one++;
two++;
three++;
return one;
return two;
else
return three;
{
Code 3:
Problem Statement
Given an integer array 'A', find the length of its longest increasing subsequence (LIS). LIS is a sub -array of the
given integer array where elements are sorted in a monotonic/strict increasing order.You need to fill in a function
that takes two inputs - integer 'n' and an integer array 'A' containing 'n'integers and returns the length of its LIS.
Input Format
First line contains integer input 'n' (1 = input1 <= 1000), size of array.
Second line contains integer array 'A' input, containing 'n' integers, ele ments of the array.
Output Format:
Return the length of its LIS
Sample Input:
3
1 32
Sample Output
2
Explanation
{1, 2} and {1, 3} are the longest increasing subsequence of {1, 3, 2}. The length of both LIS is 2.
Answer code
import java.util.*;
class Main
{
static int lis(int arr[], int n)
{
int lis[] = new int[n];
int i, j, max = 0;
for (i = 0; i < n; i++)
lis[i] = 1;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (arr[i] > arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
for (i = 0; i < n; i++)
if (max < lis[i])
max = lis[i];
return max;
}
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(lis(arr, n));
}
}
Code 4:
Problem Statement
Write a function that returns the reverse of the input array
Input Format
First line contains size of array, n (n>0).
Second line contains elements of array.
Output Format
Print the array elements in reverse order.
Sample Input
3
1 23
Sample Output
3 21
Explanation
The reverse of array {1, 2, 3} would be {3, 2, 1}.
Answer code
Python:
n=int(input())
l=list(map(int,input().split()))
p=l[::-1]
for i in range(0,len(p)):
print(p[i],end=" ")
Java:
import java.util.Scanner;
class Main
{
static void reverse_array(int arr[], int n)
{
int swap ;
for(int i = 0 ; i<n/2 ; i++)
{
swap = arr[i] ;
arr[i] = arr[n-i-1] ;
arr[n-i-1] = swap ;
}
}
System.out.print(arr[i]+" ") ;
}
}
Code 5:
Problem Statement
Write a function to remove all duplicate characters from a given string.
Note: The duplicate elements are to be removed in such a way that when reading the string fromleft to right, the
repeated element which occurs later should be removed.
Input Format
Input contains a string.
Output Format
Return a string with non-duplicate characters, i.e if you have a string as mettl then output shouldbe metl, keeping
the order of characters same.
Sample Input:
CsharpstarZ
Sample Output
CsharptZ
Explanation
On removing duplicate elements from the original string CsharpstarS, we get CsharptZ.
Answer code
import java.util.*;
class Main
{
static String removedup(String s)
{
String str="";
for(int i=0;i<s.length();i++)
{
if(str.indexOf(s.charAt(i))==-1)
str+=s.charAt(i);
}
return str;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
System.out.print(removedup(s));
}
}
Code 6:
Problem Statement
The United Nations Organization released an official document regarding the most importantevents from the
beginning of time (dated 00-00-0000) with a brief description of the events. Thedate of all the events is
mentioned in the 'DD-MM-YYYY' format.
Find the total number of distinct years referenced in the document.
Input Format
First line of input has string containing the content of the document
Output Format
Return the total number of distinct years referenced in the document.
Sample Input
UN was established on 24-10-1945. India got freedom on 15-08-1947.
Sample Output:
2
Explanation
2 distinct years, 1945 and 1947 have been referenced.
Answer code
import java.util.*;