Accenture Coding Round Cheatsheet - 1
Accenture Coding Round Cheatsheet - 1
Cheatsheet
Find the Maximum Element in an Array: Write a function that finds the
maximum element in a given array of integers.
Find the Intersection of Two Arrays: Write a function that finds the elements
present in both a given pair of arrays.
Find the Second Largest Element in an Array: Write a function that finds the
second largest element in a given array.
Merge Two Sorted Arrays: Write a function that merges two sorted arrays into
a single sorted array.
Reverse a Linked List: Write a function that reverses a given linked list.
Find the Lowest Common Ancestor in a Binary Tree: Write a function to find
the lowest common ancestor (LCA) of two given nodes in a binary tree.
Check if a Binary Tree is a Binary Search Tree (BST): Write a function that
checks if a given binary tree adheres to the properties of a BST.
Find the Level Order Traversal of a Binary Tree: Write a function that
performs a level-order traversal of a binary tree, returning the elements level
by level.
Clone a Linked List with Random Pointers: Write a function that creates a
deep copy of a linked list where each node also has a random pointer to
another node in the list.
Find the Kth Largest Element in an Array: Write a function that finds the kth
largest element in a given array.
Find All Combinations of a Sum: Write a function that finds all unique
combinations of numbers in a given array that add up to a specific target sum.
Find the Maximum and Its Index in an Array: Write a function to find the
greatest number in an array and its index.
Food Distribution Among Rats: Given the number of rats and their food
requirement, write a function to determine if the food available in different
houses is sufficient.
You are given a function, intMaxExponents(int a, int b) Find and return the
number between ‘a’ and ‘b'(range inclusive on both ends) which has the
Form an array of 1000 integers and find out the second-largest number. If
there is no second-largest number, return the value to –1.
Adam decides to do some charity work. From day 1 till day x, he will give i^2
coins to charity. On the day ‘i’ (1 < = i < = x), find the number of coins he
gives to charity.
Write a program that accepts the integer array of length ‘size’ and finds the
largest number that can be formed by permutation.
Rules
6
321754
Output:
7
Input:
bB1_89
Output:
Approach:
1. Using if condition check whether the length of string is greater than equal
to 4 or not.
2. Run a loop on the string and check if any character is a digit or not. It is a
digit if it’s between ‘0’ and ‘9’.
3. Iterate the string and check that only at least one letter should be between
‘A’ and ‘Z’, i.e it should be capital.
5. Check that the first character should not lie between ‘0’ and ‘9’.
Write a function FindMaxInArray, which will find the greatest number from
an array with its desired index? The greatest number and its desired index
should be printed in separate lines.
Input:
10
15 78 96 17 20 65 14 36 18 20
Output:
96
15
20
Output:
You are required to input the size of the matrix then the elements of matrix,
then you have to divide the main matrix in two sub matrices (even and odd)
in such a way that element at 0 index will be considered as even and
element at 1st index will be considered as odd and so on. Then you have sort
the even and odd matrices in ascending order and print the sum of second
largest number from both the matrices.
def differenceofSum(n.m)
Assumption
Sample input:
m=3
n = 10
Sample output:
19
def LargeSmallSum(arr)
The function takes an integral arr which is of the size or length of its
arguments. Return the sum of the second smallest element at odd position
‘arr’ and the second largest element at the even position.
Assumption
Array is 0 indexed.
Example
Input:
Arr: 3 2 1 7 5 4
Output:
Explanation
Write a function to validate if the provided two strings are anagrams or not.
If the two strings are anagrams, then return ‘yes’. Otherwise, return ‘no’.
Example
Input 1: Listen
Input 2: Silent
Output:
Yes
Explanation
Listen and Silent are anagrams (an anagram is a word formed by rearranging
the letters of the other word).
The given function has a string (str) and two characters, ch1 and ch2.
Execute the function in such a way that str returns to its original string, and
all the events in ch1 are replaced by ch2, and vice versa.
Example
Input:
str: apples
ch1: a
ch2: p
Output:
paales
Explanation
All the ‘a’ in the string is replaced with ‘p’. And all the ‘p’s are replaced with ‘a’.
Perform a function to reverse a string word-wise. The input here will be the
string. In the output, the last word mentioned should come as the first word
and vice versa
The function accepts an integer array ‘arr’ of size ‘n’ as its argument. Each
element of ‘arr’ represents the number of chocolates distributed to a person.
The function needs to return the minimum number of chocolates that need to
be distributed to each person so that the difference between the chocolates of
any two people is minimized.
Example:
Input: Output:
n: 5 3
arr: 10 4 12 3 1
The function accepts a character array ‘arr’ of size ‘n’ as its argument. Each
element of ‘arr’ represents the status of a parking slot, where ‘S’ represents an
empty slot and ‘X’ represents an occupied slot. The function needs to return
the maximum number of cars that can be parked in the parking lot. It is
assumed that two cars cannot occupy the same slot and cars can only park in
consecutive empty slots.
The function accepts a string ‘str’ as its argument. The function needs to
return the transformed string by replacing all occurrences of the character ‘a’
with the character ‘b’ and vice versa.
Input: Output:
The function accepts an integer array ‘arr’ of size ‘n’ as its argument. The
function needs to return the index of an equilibrium point in the array, where
the sum of elements on the left of the index is equal to the sum of elements on
the right of the index. If no equilibrium point exists, the function should return
-1.
Input: Output:
n: 5 3
arr: 1 3 5 7 3
Input: Output:
n: 5 34512
arr: 1 2 3 4 5
d: 3
The function accepts two strings ‘str1’ and ‘str2’ as its argument. The function
needs to return the index of the first occurrence of substring ‘str2’ in string
‘str1’ or -1 if the substring is not found.
Input: Output:
str2: “World”
The function accepts a string ‘str’ as its argument. The function needs to
reverse the order of the words in the string.
Example:
Input: Output:
Given an array of integers and an integer sum, find a pair of numbers (a, b) in
the array where a + b = sum.
Input: Output:
An integer sum
Input: Output:
Given an integer array, find the minimum value and its index in the array.
Example:
Input: Output:
[5, 2, 4, 1, 3] 13
Given an array of integers, find the average of all positive numbers in the
array.
Input: Output:
[5, 2, -4, 1, 3] 3
Input: Output:
[5, 2, 4, 1, 2], 2 2
Given an integer array and an integer element, check if the array contains the
element.
Input: Output: