Accenture QNS 23-08-2023

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

ACCENTURE

TECHNICAL CODING

1. Evaluated the given expression


Problem Statement
You are given a function,
int EvaluateExpression(char* expr);
The function accepts a mathematical expression ‘expr’ as parameter. Implement the function to
evaluate the given expression 'expr' and return the evaluated value.
Assumption:
● The can assume there is no space in between any of the characters of expression ‘expr’.
● Expression ‘expr’ contains only digits and operators (+, -, * and /).
Note:
● Every operation should be Integer based. e.g. : 5/2 should give 2 not 2.5
● Consider the precedence of operators while evaluating the expression, precedence of ( ‘/ or ‘*’) >
precedence of (‘+; or ‘_’).
● If ‘expr’ has multiple operators of same precedence then evaluate them from left to right.
Example:
Input:
expr: 2+3+5*4/2
Output:
15
Explanation:
2+3+5*4/2 = 2+3+10 = 15, hence 15 is the evaluated value.
The custom input format for the above case:
9
2+3+5*4/2
(The first line represents the length of the string, the second line represents the string)
Sample input:
expr: 22 +15 - 2*7/3
Sample Output:
33
The custom input format for the above case:
11
22+15-2*7/3

2. Lettered numbers
Problem Statement
You are required to implement the following function:
int LetteredNumberSum(char[] str, int len);

Page 1 of 9
ACCENTURE

The function accepts string ‘str’ (‘str1’ in case of Python) as its argument. Implement the function
which returns sum of number equivalents of each letter in the given string 'str'.
The number equivalents are as follows:
● A=1
● B = 10
● C = 100
● D = 1000
● E = 10000
● F = 100000
● G = 1000000
Assumption: ‘str’ contains upper case letters only
Note:
● Number equivalent for any letter other than (A, B, C, D, E, F, G) is 0
● Computed value lines with in integer range
● Return 0 if ‘str’ is null (None, in case of Python)
Example:
Input:
DCCBAA
Output:
1212
Explanation:
Sum = 1000 + 100 + 100 + 10 + 1 + 1 = 1212.
The custom input format for the above case:
6
DCCBAA

3. Sum of digits
Problem Statement
You are required to implement the following function:
int DifferenceSumOfDigits(int* arr, int n);
The function accepts an array ‘arr’ of ‘n’ positive integers as its argument. Let’s suppose:
f(x) = Sum of digits of an integer
You are required to calculate the value of following:
F1 = [f(arr[0]) + f(arr[1]) + f(arr[2]) + ..........+ f(arr[n-1])] %10
F2 = [(arr[0] + arr[1] + arr[2] + .........+ arr[n-1])] % 10
F = F 1 - F2
and return the value of F.
Note: n > 0
Example:

Page 2 of 9
ACCENTURE

Input:
arr: 11 14 16 10 9 8 24 5 4 3
n: 10
Output:
-4
Explanation:
The value of F1 is (1 + 1) + (1 + 4) + (1 + 6) + (1 + 0) + (9) + (8) + (2 + 4) + (5) + (4) + (3) which is
equal to 50 and (50 % 10) is 0 and value of F 2 is (11 + 14 + 16 + 10 + 9 + 8 + 24 + 5 + 4 + 3) which is
equal to 104 and (104 % 10 ) is 4, the value of F is (0 - 4), hence -4 is returned.
The custom input format for the above case:
10
11 14 16 10 9 8 24 5 4 3
(The first line represents 'n', the second line represents the elements of the array 'arr')
Sample input:
arr: 16 18 20
n: 3
Sample output:
4
The custom input format for the above case:
3
16 18 20
(The first line represents 'n', the second line represents the elements of the array 'arr')

4. In mathematics, Pascal’s triangle is a triangular array of the binomial coefficients that arises in
probability theory, combinations, and algebra
Print Pascal’s Triangle
You have been given an N, which is the no of rows.
An ordered set of integers p1, p2, …, pn, permutation p, contains n different positive integers, each of
which doesn’t exceed n. The i-th element of the permutation p will be referred to as pi. We’ll refer to
the size or length of the permutation as number n.
The number of such i (1 ≤ i < n) such permutations where pi > pi + 1 is the decreasing coefficient of
permutation p1, p2, …, pn.
There are two numbers: n and k. Printing the permutation of length n with decreasing coefficient k is
your assignment.
Input:
The decreasing coefficient and the permutation length, n, k (1 ≤ n ≤ 105, 0 ≤ k < n) are each separated
by a space on a single line.
Output:
In a single line print n space-separated integers: p1, p2, …, pn — the permutation of length n with
decreasing coefficient k.

Page 3 of 9
ACCENTURE

Input:
52
Output:
15243
Input:
30
Output:
123

5. Ishita is a curious girl and one day she decided to check whether the number is round or not.
So she wants to write an algorithm to determine if a number N is round or not.
A round number is a number defined by the following process:
-First starting with any positive integer, replace the number by the sum of the squares of its digits.
-Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle
which does not include 1.
-Those numbers for which this process ends in 1 are round numbers.
Return true if N is a rounded number false if not.

6. Good Subsequences
A subsequence of a given string is generated by deleting zero or more characters from a string, then
concatenating the remaining characters. A good subsequence is one where the frequency of each
character is the same. Given a string that consists of n Latin letters, determine how many good
subsequences it contains. Since the answer can be quite large, compute its modulo (10 9 + 7).
Note: An empty subsequence is not a good subsequence.
Example:
word = “abca”
A total of 15 non-empty subsequences can be formed from words.
The only subsequences that are not good, are “aba”, “aca” and “abca” as the frequencies of character
“a” is 2 and every other character is 1.
The total number of good subsequences = 15 – 3 = 12 and answer to the above example = 12 modulo
(109 + 7) = 12.
Function Description:
Complete the function countGoodSubsequences in the editor below.
countGoodSubsequences has the following parameter(s):
string word: a string that consists of only lowercase Latin letters
Returns:
int: the number of good subsequences modulo (109 + 7)
Constraints:
● 1 ≤ length of word ≤ 105
● word[i] is in the range [a-z]

Page 4 of 9
ACCENTURE

Input Format For Custom Testing:


Sample Case 0:
Sample Input For Custom Testing
STDIN FUNCTION
-------- --------------
abcd  word = “abcd”
Sample Output:
15
Explanation:
All of the non-empty subsequences are good subsequences.

7. Write a function that takes a number n as input and prints this pattern for 2*n-1 lines
*
**
***
****
***
**
*
Example 1:
Input:
3
Output:
*
**
***
**
*

8. String Encoder
Given a String array input1[] and another integer input2,
stringEncoder function returns a String array by following the steps:
Step 1: Consider a word from the array. Find the first, middle and last characters and concatenate
them. Lets call this as “temp”.
Step 2: Now, “temp” String from step1 has to be concatenated with itself, for “input2” number of
times. Lets call it as “answer”.
Step 3: Repeat the above two steps for each word in input1 array and store the answers in another
String array as that order.
Finally, return this String array.
Note: Input and Output are case sensitive.

Page 5 of 9
ACCENTURE

Assumptions:
1. Length of each String in input1 array will be an odd number.
2. Minimum length of each String in input1 array will be > = 3.
3. Value of input2 will be > = 1 and < = 10.
Function Prototype: String[] stringEncode(String input1[], int input2)
Sample Input/Output-1:
input1[] = (“ABC”)
input2 = 2
output[] = (“AbCAbCAbC”)
Explanation: Consider the word “AbC”. The first, middle and last characters and A,b,C.
Since input2=2, we need to concatenate “AbC” with itself for 2 times.
Hence, we get “AbCAbCAbC”. So, we need to return the String array (“AbCAbCAbC”)
Sample Input/Output-2:
input1[] = {“world”,”hello”,”madam”}
input2 = 2
output[] = {“wrdwrdwrd”, “hlohlohlo”, “mdmmdmmdm”}
Explanation: Consider the word “world”. The first, middle and last characters are w.r.d
Since input2=2, we need to concatenate “wrd” with itself for 2 times. Hence we get “wrdwrdwrd”.
Consider the next word “hello”. The first, middle and last characters are: h.l.o
Since input2=2, we need to concatenate “hlo” with itself for 2 times. Hence we get “hlohlohlo”.
Consider the next word “madam”. The first, middle and last characters are m.d.m
Since input2=2, we need to concatenate ‘mdm’ with itself for 2 times. Hence we get “mdmmdmmdm”.
Finally, putting all the results together, we need to return {“wrdwrdwrd”, hlohlohlo”,
“mdmmdmmdm”}
Sample Input/Output-3:
input1[] = {“1234567”}
input2 = 1
output[] = {147147”}
Explanation: Consider the word “1234567”. The first middle and last characters are 1,4,7.
Since inptu2=1, we need to concatenate “147” with itself for 1 time. Hence, we get “147147”.
So, we need to return this String array {“147147”}.

9. Problem Statement
You are given a string s. Your task is to modify and print the given string s in such a way that the
adjacent characters in the string don’t repeat themselves more than twice.
Note:
● You can delete the extra characters to ensure that the characters in the string don’t repeat
themselves more than twice.
● The modified string should be of maximum length.

Page 6 of 9
ACCENTURE

● Uppercase and lowercase characters should be considered different.


Input Format:
The input consists of a single line:
● The line contains the string s.
Input will be read from the STDIN by the candidate.
Output format:
Print the modified string such that the characters in the string don’t repeat themselves more than
twice.
Output will be matched to the candidate’s output printed on the STDOUT
Constraints:
● 1 ≤ String length ≤ 103.
● The string contains only lowercase and uppercase English alphabets.
Example:
Input:
abbbccdeab
Output:
abbccdeab
Explanation:
Since the input string is having the character ‘b’ thrice, we require the characters in the string doesn’t
repeat themselves more than twice, so we omit ‘b’ and desired string “abbccdeab”.

10. How to Attempt?


Find Key:
You are provided with 3 numbers : input1, input2 and input3.
Each of these are four digit numbers within the range >=1000 and <=9999.
i.e.
1000 <= input1 <= 9999
1000 <= input2 <= 9999
1000 <= input3 <= 9999
You are expected to find the Key using the below formula -
Key = (Sum of Largest digits of all the 3 numbers) + (Sum of Smallest digits of all the 3 numbers) For
e.g. if input1 = 3521, input2=2452, input3=1352, then Key = (5+5+5) + (1+2+1) = 19.
Assuming that the 3 numbers are passed to the given function. Complete the function to find and
return the key.

11. Simple Max Difference


In securities research, an analyst will look at a number of attributes for a stock. One analyst would
like to keep a record of the highest positive spread between a closing price and the closing price on
any prior day in history. Determine the maximum positive spread for a stock given its price history. If
the stock remains flat or declines for the full period, return -1.

Page 7 of 9
ACCENTURE

Example 0:
px = [7, 1, 2, 5]
Calculate the positive difference between each price and its predecessors:
● At the first quote, there is no earlier quote to compare to.
● At the second quote, there was no earlier price that was lower.
● At the third quote, the price is higher than the second quote:
○ 2-1=1
● For the fourth quote, the price is higher than the third and the second quotes:
○ 5-2=3
○ 5 - 1 = 4.
● The maximum difference is 4.
Example 1:
px = [7, 5, 3, 1]
● The price declines each quote, so there is never a difference greater than 0. In this case, return -1.
Function Description:
Complete the function maxDifference in the editor below.
maxDifference has the following parameters:
int px[n]: an array of stock prices (quotes)
Returns:
int: the maximum difference between two prices as described above
Constraints:
● 1 ≤ n ≤ 105
● -105 ≤ px[i] ≤ 105
Input Format For Custom Testing:
Sample Case 0:
Sample Input For Custom Testing:
STDIN Function
------- -----------
7  px[] size n = 7
2  px = [2, 3, 10, 2, 4, 8, 1]
3
10
2
4
8
1
Sample Output:
8

Page 8 of 9
ACCENTURE

Explanation:
Calculate the positive difference between each price quote and the previous ones:
● There is no predecessor for the first quote.
● A the second quote, the price is higher than the first quote:

Page 9 of 9

You might also like