On this page, you will find out BNY Mellon Coding Questions and Answers which will help you clear BNY Mellon Online Coding Assessments including BNY Hiring Process.
Go through this page to get all Sample BNY Mellon Coding Questions with Solutions to prepare for Online Coding Assessment and Technical Interviews of the Company.
BNY Mellon Coding Questions with Solutions
Question 1 : Weird Terminal
Problem Statement :
(Asked in OnCampus Drive August, 2023)
Here is a weird problem in Susan’s terminal. He can not write more than two words each line, if she writes more than two, it takes only 2 words and the rest are not taken. So she needs to use enter and put the rest in a new line. For a given paragraph, how many lines are needed to be written in Susan’s terminal?
Input Format: A string as the text to input in the terminal
Output Format: Number of lines written.
Constraints: Number of words <=10^7
Sample Input: How long do you have to sit dear ?
Sample Output: 4
Explanation: The writing will be: How long Do you Have to Sit dear ?
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
s=input()
n=len(s)
s1=''
for i in range(n):
if s[i]=='-':
s1+=''
continue
if s[i]=='.' or s[i]=='!' or s[i]==',' or s[i]=='?':
s1+=' '
continue
else:
s1+=s[i]
a=list(s1.split())
c=0
for i in a:
i1=0
for j in i:
if not ((ord(j)>=65 and ord(j)<=90) or (ord(j)>=97 and ord(j)<=122)):
i1=1
break
if i1==0:
c+=1
print((c-1)//2 +1)
Question 2 : Maximum Toys
(Asked in OnCampus Drive August, 2023)
In a toy shop there are a number of toys presented with several various – priced toys in a specific order. You have a limited budget and would like to select the greatest number of consecutive toys that fit within the budget. Given prices of the toys and your budget, what is the maximum number of toys that can be purchased for your child?
Example:
prices=[1,4,5,3,2,1,6]
money=6
All sub arrays that sum to less than or equal to 6 .
length 1: [1] [4] [5] [3] [2] [1] [6]
length 2: [1,4] [3,2] [2,1]
length 3: [3,2,1]
The longest of these or the maximum number of toys that can be purchased is 3.
Function description
Complete the function getMaxToys in the editor below
getMaxToys has the following parameters:
int prices[n] : the prices of the various toys
int money: the amount of money you can spend on toys
import java.util.*;
public class Main
{
public static int getMaxToys (int prices[], int money)
{
int sum = 0;
int count = 0;
int max = 0;
for (int i = 0; i < prices.length; i++)
{
sum = count = 0;
for (int j = i; j < prices.length; j++)
{
if ((sum + prices[j]) <= money)
{
sum = sum + prices[j];
count = count + 1;
max = Math.max (count, max);
}
else
{
count = 0;
sum = 0;
break;
}
}
}
return max;
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
int prices[] = new int[n];
for (int i = 0; i < n; i++)
prices[i] = sc.nextInt ();
int money = sc.nextInt ();
System.out.println (getMaxToys (prices, money));
}
}
def getMaxToys(n, arr, money):
L, H = 0, n - 1
while L <= H:
if sum(arr[L:H + 1]) <= money: break else: if arr[L] > arr[H]:
L += 1
else:
H -= 1
return H - L + 1
n = int(input())
arr = list(map(int, input().split())) # Read and split the input line
money = int(input())
print(getMaxToys(n, arr, money))
Question 03 : Device Name System
(Asked in OnCampus Drive August, 2023)
Problem statement: Rocky is a software engineer and he is creating his own operating system called “myFirst os”. myFirst os is a GUI (Graphical user interface) based operating system where everything is stored in files and folders. He is facing issues on creating unique folder names for the operating system . Help rocky to create the unique folder name for it’s os.If folder name already exists in the system and integer number is added at the name to make it unique. The integer added starts with 1 and is incremented by 1 for each new request of an existing folder name. Given a list of folder names , process all requests and return an array of corresponding folder names.
Foldername[3] =’myfirst’ already exists in our system. So Add1 at the end of the folder name i.e foldername[3] =”myfirst1″
Foldername[4 ]=’myfirst’ also already exists in our system.So add 2 at the end of the folder name i.e. foldername[4]=”myfirst2″.
Function description
Complete the function folderNameSystem In the editor below
folderNameSystem has the following parameters
string foldername[n]: an array of folder name string in the order requested
Returns:
String[n]: an array of strings usernames in the order assigned
Constraints
1<=n<=10^4
1<=length of foldername[i]<20
foldername[i] contains only lowercase english letter in the range ascii[a-z]
Input Format:
The first line contains an integer n , denoting the size of the array usernames Each line i of the n subsequent lines (where i<=0<=n) contains a string usernames[i] representing a username request in the order received.
Sample case 0
4
home
download
first
first
Sample Output 0
home
download
first
first1
Explanation 0
foldername[0] = ‘home’ is unique
foldername[1]=’download’ is unique
foldername[2]= ‘first’ is unique
foldername[3]=’first’ is already existing . so add 1 to it and it become first1
n=int(input())
arr = []
ans = ""
for i in range(n):
arr.append(input())
print()
for i in range(n):
if arr[:i+1].count(arr[i])>1:
ans = arr[i]+str(arr[:i+1].count(arr[i])-1)
else:
ans = arr[i]
print(ans)
Question 4: Amusement park
(Asked in OnCampus Drive July, 2023)
Problem Statement – Akshay loves to go to WONDERLA , an amusement park. They are offering students who can code well with some discount. Our task is to reduce the cost of the ticket as low as possible.
The cost of tickets can be removed by removing the digits from the price given. They will give some k turns to remove the digits from the price of the ticket. Your task is to help Akshay in coding a program that can help him to reduce the cost of a ticket by removing the digits from its price and getting the maximum possible discount.
Note – You cannot make the cost of a ticket zero. For eg -: If the cost of a ticket is 100, and you have 2 turns to reduce the price, the final price will be 1 and not zero.
Constraints:
1 <= number of tickets <= 10^5
1 <= K < Number of digits in Price of ticket
Input Format for Custom Testing:
The first line contains a string,Tickets, denoting the given cost of each ticket.
The next line contains an integer, K, denoting the number of tickets that is to be removed.
#include<bits/stdc++.h>
using namespace std;
int smallestNumber(string num, int k) {
if (num.length() <= k) {
return 0;
}
unordered_map pos;
for (int i = 0; i < num.length(); i++) {
pos[num[i]] = i;
}
string temp = num;
// Sort the characters in ascending order
sort(num.begin(), num.end());
// Get the required substring
string ans = num.substr(0, num.length() - k);
vector v;
for (int i = 0; i < ans.length(); i++) {
v.push_back(pos[ans[i]]);
}
// Sort the positions
sort(v.begin(), v.end());
string ret;
for (int i = 0; i < v.size(); i++) {
ret += temp[v[i]];
}
int final = stoi(ret);
return final;
}
int main() {
string s;
cin >> s;
int k;
cin >> k;
int ans = smallestNumber(s, k) % static_cast(pow(10, 9) + 7);
cout << ans;
return 0;
}
import sys
n=input()
k=int(input())
n1=len(n)
if len(n)<=k:
print(0)
sys.exit()
a=''
i=0
while i < (n1-1) and k>0:
if int(n[i])>int(n[i+1]):
i+=1
k-=1
continue
else:
a+=n[i]
i+=1
a+=n[i]
i+=1
if k>0:
a=a[:-k]
if i<=(n1-1):
while i < n1:
a+=n[i]
i+=1
print(int(a)%((10**9)+7))
import java.util.*;
class Main
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
String s = sc.nextLine ();
int k = sc.nextInt ();
int ans;
System.out.println (smallestNumber (s, k) % (int) (1e9 + 7));
}
static int smallestNumber (String num, int k)
{
if (num.length () <= k)
return 0;
HashMap < Character, Integer > pos = new HashMap <> ();
for (int i = 0; i < num.length (); i++)
{
pos.put (num.charAt (i), i);
}
String temp = num;
num = sortString (num);
String ans = num.substring (0, num.length () - k);
ArrayList < Integer > v = new ArrayList <> ();
for (int i = 0; i < ans.length (); i++)
{
v.add (pos.get (ans.charAt (i)));
}
Collections.sort (v);
String ret = "";
for (int i = 0; i < v.size (); i++)
{
ret += temp.charAt (v.get (i));
}
int result = Integer.parseInt ("" + ret);
return result;
}
public static String sortString (String inputString)
{
char tempArray[] = inputString.toCharArray ();
Arrays.sort (tempArray);
return new String (tempArray);
}
}
Question 05 : PrepInsta Name : Borrow Number
(Asked in OnCampus Drive July, 2023)
Problem statement : You have two numbers number1 and number2, your job is to check the number of borrow operations needed for subtraction of number1 from number2. If the subtraction is not possible then return the string not possible.
Stephan is a vampire. And he is fighting with his brother Damon. Vampires get energy from human bloods, so they need to feed on human blood, killing the human beings. Stephan is also less inhuman, so he will like to take less life in his hand. Now all the people’s blood has some power, which increases the powers of the Vampire. Stephan just needs to be more powerful than Damon, killing the least human possible. Tell the total power Steohan will have after drinking the bloods before the battle. Note that: Damon is a beast, so no human being will be left after Damon drinks everyone’s blood. But Stephan always comes early in the town.
Input Format : First line with the number of people in the town, n. Second line with a string with n characters, denoting the one digit power in every blood.
Output Format : Total minimum power Stephan will gather before the battle.
Constraints : n<=10^4
Sample input : 6 093212
Sample output: 9
Explanation : Stephan riches the town, drinks the blood with power 9. Now Damon cannot reach 9 by drinking all the other bloods.
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, sum = 0, sum1 = 0;
cin >> n;
string s;
cin >> s;
sort(s.begin(), s.end(), greater < char > ());
for (auto i: s) sum += (i - '0');
for (auto i: s) {
if (sum1 > sum) break;
sum1 += (i - '0');
sum -= i - '0';
}
cout << sum1;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String str = sc.next();
char arr[] = str.toCharArray();
int a[] = new int[arr.length];
for (int i = 0; i < a.length; i++)
a[i] = Integer.parseInt(arr[i] + "");
Arrays.sort(a);
int sum = 0;
for (int i = 0; i < a.length; i++)
sum = sum + a[i];
int sumA = 0;
int sumB = sum;
ArrayList < Integer > subsetA = new ArrayList < Integer > ();
for (int i = a.length - 1; i >= 0; i--) {
sumA = sumA + a[i];
sumB = sumB - a[i];
subsetA.add(a[i]);
if (sumA > sumB)
break;
}
Iterator itr = subsetA.iterator();
while (itr.hasNext()) {
System.out.print((Integer) itr.next());
}
}
}
n = int(input())
ar = input()
sorted(ar, reverse=True)
br = []
s = 0
aa = []
for i in ar:
aa.append(int(i))
su = sum(aa)
while s <= su:
s += aa[0]
su = su - (aa[0])
br.append(aa.pop(0))
print(sum(br))
Question 7: Array Subarray
(Asked in OnCampus Drive July, 2023)
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 :
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
n=int(input())
arr=[]
for i in range(n):
arr.append(int(input()))
s,a=0,0
for i in arr:
s=s+i
if(s < 1):
a=a+(-1*s)
s=0
print(a)
Question 8 : Coin Game (R->Medium)
(Asked in OnCampus Drive July, 2023)
Problem Statement :
Raman was playing a game, he starts with x coins. Now in every step, he wins and loses and he has to get the money or pay the money as needed. He came in contact with a psychic who can see the future and the Psychic predicted the outcomes after each step. Now Raman wants to start the game with the minimum wage where he doesn’t run out of money. Help Raman to find what money he should start with. The only rule to keep playing is not going in a credit situation.
Input Format:
First line with n, number of steps in the game Next n lines, n integers denoting outcomes of every game. Positive means winning and negative means losing that money.
Output Format:
One single integer denoting the minimum amount to start with
Constraints:
Number of steps<=10^9 -1000<=Money needed in each step<=1000
Sample Input:
4 2 -9 15 2
Sample Output:
7
Explanation:
If he starts with 7 rupees, then after steps : 7 ->9 -> 0-> 15 -> 17.
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int sum = 0, ans = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (sum < 1) {
sum = -sum;
ans += sum;
sum = 0;
}
}
cout << ans << endl;
}
import java.util.*;
public class Main {
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();
int sum = 0, ans = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
if (sum < 1) {
sum = -sum;
ans += sum + 1;
sum = 1;
}
}
System.out.println(ans);
}
}
n = int(input())
arr = []
for i in range(n):
arr.append(int(input()))
s, a = 0, 0
for i in arr:
s = s + i
if s < 1:
a = a + (-1 * s)
s = 0
print(a)
Question 9 : Coloured Zenga
(Asked in OnCampus Drive July, 2023)
Problem Statement :
Rahul is playing a game, wherein he has multiple coloured wooden blocks, stacked one above the other, his task is to remove all the wooden blocks from the stack, without letting it fall and in the minimum number of steps. He can remove one block of a colour at a time, but he can remove multiple blocks of the same colour together. Determine the minimum number of steps in which he can perform this task.
For example, if you remove [red,red] from (white,red,red,white), the resulting array is [white,white].
Note- there are only two colour blocks – red and white
Function description :
Complete the minMoves function in the provided editor. It contains the following parameters:
Parameters:
Name
Type
Description
N
Integer
No. of Wooden blocks
Array[ ]
Integer Array
Array of Blocks.
Input format :
The first line contains an integer n denoting the number of blocks. Each n line denotes the colour of the wooden block .
Constraints : 1<=n<=700 0<=a[i]<=1
Sample input 1 :
4 red white white red
Sample Output 2 :
2
Explanation :
Remove [white,white] first The array will be [red,red] The remaining numbers can be removed in one strap .
Sample Input 1:
4 white red white red
Sample Output 1:
3
Sample Explanation: 0 The steps are [white,red,white,red]->[red,white,red]->[red,red]->[]. Therefore the answer is 3.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String arr[] = new String[n];
for (int i = 0; i < n; i++)
arr[i] = sc.next();
int a = 0, b = 0;
boolean flag = false;
for (int i = 0; i < n; i++) {
if (arr[i].equals("white")) {
if (!flag)
flag = true;
} else {
if (flag) {
a++;
flag = false;
}
}
}
if (flag)
a++;
flag = false;
for (int i = 0; i < n; i++) {
if (arr[i].equals("red")) {
if (!flag)
flag = true;
} else {
if (flag) {
b++;
flag = false;
}
}
if (flag)
b++;
}
System.out.println(Math.min(a + 1, b + 1));
}
}
n = int(input())
arr = []
for i in range(n):
arr.append(input())
a, b = 0, 0
start = False
for i in range(n):
if arr[i] == "white":
if not start:
start = True
else:
if start:
a += 1
start = False
if start:
a += 1
start = False
for i in range(n):
if arr[i] == "red":
if not start:
start = True
else:
if start:
b += 1
start = False
if start:
b += 1
print(min(a + 1, b + 1))
Question 10 :A Good Prime Number
(Asked in OnCampus Drive July, 2023)
Problem Statement :
A prime number is a number which is divisible by one and itself. Also a number is called a good prime number if the sum of its digits is a prime number. For example a number 23 is a good prime number because the sum of 2 and 3 ( 2+3=5) is 5 which is a prime number. You are given an integer K. Your task is to find the kth good prime number that is greater than a provided number N.
For example , 232 is a good prime number since the sum of all digits is 7 which is a prime number whereas 235 is not a good prime number.
Input format :
The first line contains an integer N.
The next line contains an integer K.
Output format :
A single integer which is a Kth good prime number that is greater than a provided number N.
Constraints :
1<=N<=10^5
1<=K<<=10^5
Sample Input 1:
4 4
Sample Output 1:
12
Explanation :
Good prime numbers starting from 4 are 5,7,11(1+1=2 which is prime number),12(1+2=3 which is prime number),14(1+4=5 which is a prime number) and so on. Because the sum of digits of an individual number is a prime number And 4 th good prime number is 12 in this series.Hence the output is 12.
Sample Input 2:
17 5
Sample Output 2:
29
Explanation :
Good prime numbers starting from 17 are 20,21,23,25,29…and the 5th prime number is 29.Hence the output is 29.
#include<bits/stdc++.h>
using namespace std;
bool isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i += 6)
if ((n % i == 0) || (n % (i + 2) == 0))
return false;
return true;
}
int solve(int n, int k) {
int c = 0;
vector list;
// Find k numbers greater than n with the property that the sum of their digits is prime
for (int i = n + 1; c < k; i++) {
int temp = i;
int sum = 0;
while (temp != 0) {
sum += temp % 10;
temp /= 10;
}
if (isPrime(sum)) {
list.push_back(i);
c++;
}
}
return list[k - 1];
}
int main() {
int n;
cin >> n;
int k;
cin >> k;
cout << solve(n, k);
return 0;
}
import java.util.*;
class Main {
public static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if ((n % i == 0) || (n % (i + 2) == 0))
return false;
return true;
}
public static int solve(int n, int k) {
int c = 0;
ArrayList < Integer > list = new ArrayList < > ();
for (int i = n + 1; c < k; i++) {
int temp = i;
int sum = 0;
while (temp != 0) {
sum = sum + temp % 10;
temp = temp / 10;
}
if (isPrime(sum)) {
list.add(i);
c++;
}
}
return list.get(k - 1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
System.out.println(solve(n, k));
}
}
import math
def isPrime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(math.sqrt(n)) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
def solve(n, k):
c = 0
list = []
for i in range(n + 1, 1000000000):
temp = i
sum = 0
while temp != 0:
sum = sum + temp % 10
temp = temp // 10
if isPrime(sum):
list.append(i)
c += 1
if c == k:
break
return list[k - 1]
n, k = map(int, input().split())
print(solve(n, k))
FAQs on BNY Mellon Coding Questions
Question 1: What questions are asked in coding test?
DSA Based Coding Questions are Included in both BNY Mellon Coding Test.
Question 2: What is BNY Mellon Code Diva ?
BNY Mellon Code Diva is a hiring challenge consist of 2 Technical Test conducted by BNY Mellon, to hire Women.
Question 3: What are the benefits of BNY Mellon Code Diva?
After clearing BNY Mellon Code Vita selected Women will get Tech Internships and Full Time Positions.
Login/Signup to comment