Darwinbox Coding Questions Round 2
Sample Darwinbox Coding Questions Round 2
Here, Darwinbox Coding Questions Round 2 page will help you to get Sample Darwinbox Coding Questions which will help you to prepare for Darwinbox Coding Test 2 included in Darwinbox Recruitment Process.
Go through this page to get all the sample Darwinbox Coding Questions for Round 2 as well as FAQs related to Darwinbox Recruitment Process at the end of this page.

Sample Darwinbox Coding Questions Round 2
Question 1: Loan Offer
Problem Description
Question – : There are two banks – Bank A and Bank B. Their interest rates vary. You have received offers from both banks in terms of the annual rate of interest, tenure, and variations of the rate of interest over the entire tenure.You have to choose the offer which costs you least interest and reject the other. Do the computation and make a wise choice.
The loan repayment happens at a monthly frequency and Equated Monthly Installment (EMI) is calculated using the formula given below :
EMI = loanAmount * monthlyInterestRate / ( 1 – 1 / (1 + monthlyInterestRate)^(numberOfYears * 12))
Constraints:
- 1 <= P <= 1000000
- 1 <=T <= 50
- 1<= N1 <= 30
- 1<= N2 <= 30
Input Format:
- First line: P principal (Loan Amount)
- Second line: T Total Tenure (in years).
- Third Line: N1 is the number of slabs of interest rates for a given period by Bank A. First slab starts from the first year and the second slab starts from the end of the first slab and so on.
- Next N1 line will contain the interest rate and their period.
- After N1 lines we will receive N2 viz. the number of slabs offered by the second bank.
- Next N2 lines are the number of slabs of interest rates for a given period by Bank B. The first slab starts from the first year and the second slab starts from the end of the first slab and so on.
- The period and rate will be delimited by single white space.
Output Format: Your decision either Bank A or Bank B.
Explanation:
- Example 1
- Input
- 10000
- 20
- 3
- 5 9.5
- 10 9.6
- 5 8.5
- 3
- 10 6.9
- 5 8.5
- 5 7.9
- Output: Bank B
- Example 2
- Input
- 500000
- 26
- 3
- 13 9.5
- 3 6.9
- 10 5.6
- 3
- 14 8.5
- 6 7.4
- 6 9.6
- Output: Bank A
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
#include<bits/stdc++.h> using namespace std; int main() { double p,s,mi,sum,emi,bank[5],sq; int y,n,k,i,yrs,l=0; cin>>p; cin>>y; for(k=0;k<2;k++) { cin>>n; sum=0; for(i=0;i<n;i++) { cin>>yrs; cin>>s; mi=0; sq=pow((1+s),yrs*12); emi= (p*(s))/(1-1/sq); sum= sum + emi; } bank[l++]=sum; } if(bank[0]<bank[1]) cout<<("Bank A"); else cout<<("Bank B"); return 0; }
Output 10000 20 3 5 9.5 10 9.6 5 8.5 3 10 6.9 5 8.5 5 7.9 Bank B
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); double p,s,mi,sum,emi,sq; int y,n,k,yrs,l=0; double[] bank = new double[5]; System.out.println("Enter the principal amount"); p = sc.nextDouble(); System.out.println("Enter tenature year"); y = sc.nextInt(); for (k = 0; k < 2; k++) { System.out.println("Enter the no of slabs"); n = sc.nextInt(); sum=0; for (int i = 0; i < n; i++) { System.out.println("Enter the period :"); yrs = sc.nextInt(); System.out.println("Enter the interest :"); s = sc.nextDouble(); mi=0; sq=Math.pow((1+s), yrs*12); emi=(p*(s))/(1-1/sq); sum=sum+emi; } bank[l++]=sum; } if(bank[0]<bank[1]) System.out.println("Bank A"); else System.out.println("Bank B"); } }
Output 10000 20 3 5 9.5 10 9.6 5 8.5 3 10 6.9 5 8.5 5 7.9 Bank B
bank = [] principal = int(input()) year = int(input() for i in range(0, 2): # 2 Banks installments = int(input()) sum = 0 for i in range(0, installments): time, roi = [float(i) for i in input().split()] square = pow((1+roi), time*12) emi = (principal*(roi)/(1-1/square)) sum = sum + emi bank.append(sum) if bank[0] < bank[1]: print("Bank A") else: print("Bank B")
Output 10000 20 3 5 9.5 10 9.6 5 8.5 3 10 6.9 5 8.5 5 7.9 Bank B
Question 2:
A taxi can take multiple passengers to the railway station at the same time.On the way back to the starting point,the taxi driver may pick up additional passengers for his next trip to the airport.A map of passenger location has been created,represented as a square matrix.
The Matrix is filled with cells,and each cell will have an initial value as follows:
- A value greater than or equal to zero represents a path.
- A value equal to 1 represents a passenger.
- A value equal to -1 represents an obstruction.
The rules of motion of taxi are as follows:
- The Taxi driver starts at (0,0) and the railway station is at (n-1,n-1).Movement towards the railway station is right or down,through valid path cells.
- After reaching (n-1,n-1) the taxi driver travels back to (0,0) by travelling left or up through valid path cells.
- When passing through a path cell containing a passenger,the passenger is picked up.once the rider is picked up the cell becomes an empty path cell.
- If there is no valid path between (0,0) and (n-1,n-1),then no passenger can be picked.
- The goal is to collect as many passengers as possible so that the driver can maximize his earnings.
For example consider the following grid,
0 1
-1 0
Start at top left corner.Move right one collecting a passenger. Move down one to the destination.Cell (1,0) is blocked,So the return path is the reverse of the path to the airport.All Paths have been explored and one passenger is collected.
Returns:
- Int:maximum number of passengers that can be collected.
Sample Input 0
- 4 -> size n = 4
- 4 -> size m = 4
- 0 0 0 1 -> mat
- 1 0 0 0
- 0 0 0 0
- 0 0 0 0
Output 0
- 2
Explanation 0
The driver can contain a maximum of 2 passengers by taking the following path
(0,0) → (0,1) → (0,2) → (0,3) → (1,3) → (2,3) → (3,3) → (3,2) → (3,1) → (3,0) → (2,0) → (1,0) → (0,0)
Sample Input 1
STD IN Function
———— ————-
- 3 → size n=3
- 3 → size m=3
- 0 1 -1 → mat
- 1 0 -1
- 1 1 1
Sample Output 1
- 5
Explanation 1
The driver can contain a maximum of 5 passengers by taking the following path
(0,0) → (0,1) → (1,1) → (2,1) → (2,2) → (2,1) → (2,0) → (1,0) → (0,0)
#include<bits/stdc++.h> using namespace std; int n, m; int mat[105][105]; map<pair<int, pair<int, int>>, int> dp; bool isValid(int i, int j) { if (mat[i][j] == -1) return false; if (i < 0 || i >= n) return false; if (j < 0 || j >= m) return false; return true; } int solve(int i, int j, int x, int y) { if (!isValid(i, j)) { return INT_MIN; } if (!isValid(x, y)) { return INT_MIN; } if (i == n - 1 && x == n - 1 && j == m - 1 && y == m - 1) { if (mat[i][j] == 1) { return 1; } else { return 0; } } if (dp.find({i, {j, x}}) != dp.end()) return dp[{i, {j, x}}]; int cur = 0; if (i == x && j == y) { if (mat[i][j] == 1) cur = 1; } else { if (mat[i][j] == 1) cur++; if (mat[x][y] == 1) cur++; } int op1 = solve(i + 1, j, x + 1, y); int op2 = solve(i, j + 1, x, y + 1); int op3 = solve(i + 1, j, x, y + 1); int op4 = solve(i, j + 1, x + 1, y); int ans = cur + max(op1, max(op2, max(op3, op4))); return dp[{i, {j, x}}] = ans; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> mat[i][j]; int ans = solve(0, 0, 0, 0); if (ans >= 0) cout << solve(0, 0, 0, 0) << endl; else cout << -1 << endl; return 0; }
import java.util.*; class Main { public static int cost(int grid[][], int row1, int col1, int row2, int col2) { if (row1 == row2 && col1 == col2) { if (grid[row1][col1] == 1) return 1; return 0; } int ans = 0; if (grid[row1][col1] == 1) ans++; if (grid[row2][col2] == 1) ans++; return ans; } public static int solve(int n, int m, int grid[][], int dp[][][], int row1, int col1, int row2) { int col2 = (row1 + col1) - (row2); if (row1 == n - 1 && col1 == m - 1 && row2 == n - 1 && col2 == m - 1) return 0; if (row1 >= n || col1 >= m || row2 >= n || col2 >= m) return -1 * Integer.MAX_VALUE; if (dp[row1][col1][row2] != -1) return dp[row1][col1][row2]; int ch1 = -1 * Integer.MAX_VALUE, ch2 = -1 * Integer.MAX_VALUE; int ch3 = -1 * Integer.MAX_VALUE, ch4 = -1 * Integer.MAX_VALUE; if (grid[row1][col1 + 1] != -1 && grid[row2 + 1][col2] != -1) ch1 = cost(grid, row1, col1 + 1, row2 + 1, col2) + solve(n, m, grid, dp, row1, col1 + 1, row2 + 1); if (grid[row1][col1 + 1] != -1 && grid[row2][col2 + 1] != -1) ch2 = cost(grid, row1, col1 + 1, row2, col2 + 1) + solve(n, m, grid, dp, row1, col1 + 1, row2); if (grid[row1 + 1][col1] != -1 && grid[row2][col2 + 1] != -1) ch3 = cost(grid, row1 + 1, col1, row2, col2 + 1) + solve(n, m, grid, dp, row1 + 1, col1, row2); if (grid[row1 + 1][col1] != -1 && grid[row2 + 1][col2] != -1) ch4 = cost(grid, row1 + 1, col1, row2 + 1, col2) + solve(n, m, grid, dp, row1 + 1, col1, row2 + 1); return dp[row1][col1][row2] = Math.max(ch1, Math.max(ch2, Math.max(ch3, ch4))); } public static void initializeDp(int dp[][][], int item) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) for (int k = 0; k < 5; k++) dp[i][j][k] = item; } } public static int collectMax(int n, int m, int grid[][]) { int ans = 0; int dp[][][] = new int[6][6][6]; initializeDp(dp, -1); if (grid[n - 1][m - 1] == -1 || grid[0][0] == -1) ans = -1 * Integer.MAX_VALUE; if (grid[0][0] == 1) ans++; grid[0][0] = 0; if (grid[n - 1][m - 1] == 1) ans++; grid[n - 1][m - 1] = 0; ans += solve(n, m, grid, dp, 0, 0, 0); return Math.max(ans, 0); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int arr[][] = new int[n + 1][m + 1]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) arr[i][j] = sc.nextInt(); System.out.println(collectMax(n, m, arr)); } }
Question 3: Majority Element
The majority element in an array is defined as the element that appears more than ⌊n/2⌋ times, where n is the length of the array.
In other words, it is the element that occurs most frequently and makes up more than half of the array.
Given an array of integers, the task is to find the majority element and return it. If there is no majority element, If there is no majority element, the algorithm should indicate that.
Examples:
Example 1:
Input: [3, 3, 4, 2, 4, 4, 2, 4, 4]
Output: 4
Explanation:
In the given array, the number 4 appears 5 times, which is more than half of the array size (9/2 = 4.5). Therefore, 4 is the majority element.
Example 2:
Input: [1, 2, 3, 4, 4, 4, 4]
Output: 4
Explanation:
In this case, the number 4 appears 4 times, which is more than half of the array size (7/2 = 3.5). Thus, 4 is the majority element.
Example 3:
Input: [1, 2, 3, 4, 5]
Output: -1
Explanation:
There is no majority element in this array since no number appears more than half of the array size (5/2 = 2.5).
Example 4:
Input: [2, 2, 2, 3, 3, 4, 4, 4, 4]
Output: -1
Explanation:
In this case, although the number 4 appears 4 times, it does not occur more than half of the array size (9/2 = 4.5).
Hence, there is no majority element.
#include<bits/stdc++.h> int majorityElement(std::vector& nums) { int count = 0; int candidate = 0; for (int num : nums) { if (count == 0) { candidate = num; count = 1; } else if (num == candidate) { count++; } else { count--; } } return candidate; } int validateMajorityElement(std::vector& nums, int candidate) { int count = 0; for (int num : nums) { if (num == candidate) { count++; } } if (count > nums.size() / 2) { return candidate; } else { return -1; // No majority element found } } int findMajorityElement(std::vector& nums) { int candidate = majorityElement(nums); return validateMajorityElement(nums, candidate); } int main() { std::vector nums = {3, 3, 4, 2, 4, 4, 2, 4, 4}; int result = findMajorityElement(nums); if (result != -1) { std::cout << "Majority Element: " << result << std::endl; } else { std::cout << "No majority element found." << std::endl; } return 0; }
def majority_element(nums): count = 0 candidate = None for num in nums: if count == 0: candidate = num count = 1 elif num == candidate: count += 1 else: count -= 1 return candidate def validate_majority_element(nums, candidate): count = 0 for num in nums: if num == candidate: count += 1 if count > len(nums) // 2: return candidate else: return None def find_majority_element(nums): candidate = majority_element(nums) return validate_majority_element(nums, candidate) # Example usage nums = [3, 3, 4, 2, 4, 4, 2, 4, 9] result = find_majority_element(nums) if result is not None: print("Majority Element:", result) else: print("No majority element found.")
import java.util.HashMap; import java.util.Map; public class Main { public static int majorityElement(int[] nums) { int count = 0; int candidate = 0; for (int num : nums) { if (count == 0) { candidate = num; count = 1; } else if (num == candidate) { count++; } else { count--; } } return candidate; } public static int validateMajorityElement(int[] nums, int candidate) { int count = 0; for (int num : nums) { if (num == candidate) { count++; } } if (count > nums.length / 2) { return candidate; } else { return -1; // No majority element found } } public static int findMajorityElement(int[] nums) { int candidate = majorityElement(nums); return validateMajorityElement(nums, candidate); } public static void main(String[] args) { int[] nums = {3, 3, 4, 2, 4, 4, 2, 4, 4}; int result = findMajorityElement(nums); if (result != -1) { System.out.println("Majority Element: " + result); } else { System.out.println("No majority element found."); } } }
Question 4: Parallel Columbus
Problem Statement – Nobel Prize-winning Austrian-Irish physicist Erwin Schrödinger developed a machine and brought as many Christopher Columbus from as many parallel universes as he could. Actually, he was quite amused by the fact that Columbus tried to find India and got America. He planned to dig it further.
Though totally for research purposes, he made a grid of size n X m, and planted some people of America in a position (x,y) [in 1 based indexing of the grid], and then planted you with some of your friends in the (n,m) position of the grid. Now he gathered all the Columbus in 1,1 positions and started a race.
Given the values for n, m, x, y, you have to tell how many different Columbus(s) together will explore you as India for the first time.
Remember, the Columbus who will reach to the people of America, will be thinking that as India and hence wont come further.
Function Description:
Complete the markgame function in the editor below. It has the following parameter(s):
Parameters:
Name | Type | Description |
n | Integer | The number of rows in the grid. |
m | Integer | The number of columns in the grid. |
x | Integer | The American cell’s Row. |
y | Integer | The American cell’s Column. |
Constraints:
- 1 <= n <= 10^2
- 1 <= m <= 10^2
- 1 <= x <= n
- 1 <= y <= m
Input Format:
- The first line contains an integer, n, denoting the number of rows in the grid.
- The next line contains an integer m, denoting the number of columns in the grid.
- The next line contains an integer, x, denoting the American cell’s row.
- The next line contains an integer, y, denoting the American cell’s column.
Sample Cases
Sample Input 1
2
2
2
1
Sample Output 1
1
Explanation
The only way possible is (1,1) ->(2,1) -> (2,2), so the answer is 1.
#include<bits/stdc++.h> using namespace std; unordered_map<int,long long int> f; long long int Fact(int n) { if(f[n]) return f[n]; return f[n]=n*Fact(n-1); } int main() { int n,m,x,y; cin>>n>>m>>x>>y; n-=1;m-=1;x-=1;y-=1; f[0]=f[1]=1; int p=(Fact(m+n)/(Fact(m)*Fact(n))); int imp=((Fact(x+y)/(Fact(x)*Fact(y)))*(Fact(m-x+n-y)/(Fact(m-x)*Fact(n-y)))); cout<<p-imp; }
import math n=int(input())-1 m=int(input())-1 x=int(input())-1 y=int(input())-1 ans=math.factorial(n+m) ans=ans//(math.factorial(n)) ans=ans//(math.factorial(m)) ans1=math.factorial(x+y) ans1=ans1//(math.factorial(x)) ans1=ans1//(math.factorial(y)) x1=n-x y1=m-y ans2=math.factorial(x1+y1) ans2=ans2//(math.factorial(x1)) ans2=ans2//(math.factorial(y1)) print(ans-(ans1*ans2))
import java.util.*; class Main { static int f[] = new int[1000]; static int Fact(int n) { if(f[n]==1) return f[n]; return f[n]=n*Fact(n-1); } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int m = sc.nextInt (); int x = sc.nextInt (); int y = sc.nextInt (); n-=1;m-=1;x-=1;y-=1; f[0]=f[1]=1; int p=(Fact(m+n)/(Fact(m)*Fact(n))); int imp=((Fact(x+y)/(Fact(x)*Fact(y)))*(Fact(m-x+n-y)/(Fact(m-x)*Fact(n-y)))); System.out.println(p-imp); } }
Question 5 : Array Subarray
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
#include <bits/stdc++.h> using namespace std; vector < int > arr; int prevmin=-1; int flag=0; int x,n,q; int sorting(int start,int end) { if(start+1==n) {start=0;end=end-n;} if(start==end) return arr[start]; return min(arr[start],sorting(start+1,end)); } int func(int start,int end) { if(flag==0) {flag++;return prevmin=sorting(start,end);} if(arr[start-1]==prevmin) return prevmin; return prevmin=(arr[end] <= prevmin)?prevmin:sorting(start,end); } int main() { cin >> x >> n; int ans=0; for(int i=0;i < n;i++) {cin >> q;arr.push_back(q);} for(int i=0;i < n;i++) { ans=max(ans,func(i,i+x-1)); } cout << ans; }
import java.util.*; public class DiskSpace { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int x=sc.nextInt(); int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i < n;i++) arr[i]=sc.nextInt(); int min=Integer.MAX_VALUE; int max=Integer.MIN_VALUE; for(int i=0;i <= n-x;i++) { min=Integer.MAX_VALUE; for(int j=i;j < (i+x);j++) min=Math.min(min,arr[j]); max=Math.max(min,max); } System.out.println(max); } }
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 6 : Maximum Toys
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
Returns
Int the maximum number of toys you can purchase
Constraints
1<=n<=10^5
1<=price[i]<=100
1<=money<=10^6
Sample case
Sample input 0
7->n=7
1-> price[]=[1,4,5,3,2,1,6]
4
5
3
2
1
6
Sample Output
6 ->money=6
#include<bits/stdc++.h> using namespace std; int getMaxToys(vector& prices, int money) { int sum = 0; int count = 0; int maxCount = 0; for (int i = 0; i < prices.size(); i++) { sum = count = 0; for (int j = i; j < prices.size(); j++) { if ((sum + prices[j]) <= money) { sum += prices[j]; count++; maxCount = max(count, maxCount); } else { count = 0; sum = 0; break; } } } return maxCount; } int main() { int n; cin >> n; vector prices(n); for (int i = 0; i < n; i++) { cin >> prices[i]; } int money; cin >> money; cout << getMaxToys(prices, money) << endl; return 0; }
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 7 : Minimum Number of Clicks
Problem Statement – Sahil watches TV all day and gets bored. He started playing this dumb game of identifying minimum number of inputs needed to reach a channel. As his cousin, you have to help him, but you live far from his house. So you decide to write a code that will ask Sahil for some inputs and give outputs respectively.
Here are the problems you need to keep in mind,
- There are 13 buttons on his remote: 10 buttons for the numbers (0-9) to form integers denoting respective channel index, “Up channel” button and “ Down channel” button for going i +1th channel and i-1th channel from i respectively, and a “Last viewed” button to see what’s the last channel before it.
- The number buttons allow you to jump directly to a specific channel (Ex: to go to channel 172 by typing 1,7,2).
- If the channel which you are in is ith and that is the max channel index possible, by Up channel, you will reach the first channel possible. Same goes for the down channel button. You can go to the highest channel possible if you go down from the lowest channel possible.
Sahil can get from one channel to the next in one of the two ways.
Sahil’s parents have set some parental control on some channels on Aniruth’s television. The “Up Channel “ and “Down buttons” buttons skip these channels as these channels are not viewable.
Given a list of channels to view, the lowest channel, the highest channel, and a list of blocked channels, your program should return the minimum number of clicks necessary to get through all the shows that Anirudh would like to match.
Input Format
- First line is the lowest Channel
- Second-line is the highest Channel
- Followed by a number of blocked channels B,
- and the next B lines contain the actual blocked channels.
- Followed by the number of Channels to view V, and the next V lines contain the actual channels to view.
Constraints
- The lowest channel on the television will be greater than 0. and less than or equal to 10,000.
- The highest channel on the television will be greater than or equal to the lowest channel. and less than or equal to 10.000.
- The list of channels that are blocked on Anirudh’s television. All the channels in this list will be valid channels (greater than or equal to lowest channel, less than or equal 1 to highest channel). Duplicates may be Ignored. The blocked list can be a maximum of 40 channels.
- The sequence that Sahil must view contains between 1 and 50 elements. inclusive. All channels in this sequence are not in the blocked list and are between lowest channel and highest channel. Inclusive.
Sample Input 0:
1
20
2
18
19
5
15
14
17
1
17
Sample output 0:
7
#include<bits/stdc++.h> using namespace std; unordered_map<int,int> m; int l,u; int util(int a,int b) { if(a==b) return 0; if(m[a]) return util(a+1,b); return 1+util(a+1,b); } int func(int b,int prev) { if(b<prev) return min(util(prev,u)+util(l,b)+1,util(b,prev)); else return min(util(prev,b),util(l,b)+util(prev,u)+1); } int main() { int flag=0,ans=0,prev,prev2; cin>>l>>u; int bn,b; cin>>bn; while(bn--){ cin>>b;m[b]++; } cin>>bn; while(bn--) { cin>>b; if(b>9 && flag==0) {ans+=2;flag++;prev=b;} else if(flag==0) {ans+=1;flag++;prev=b;} else if(prev2==b) {prev2=prev;prev=b;ans++;} else { ans+=min(b>9?2:1,func(prev,b));prev2=prev;prev=b; } } cout<< ans; }
import java.util.*; public class GameOfClicks { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int l=sc.nextInt(); int h=sc.nextInt(); int b=sc.nextInt(); Listbl=new ArrayList<>(); for(int i=0;i<b;i++){ bl.add(sc.nextInt()); } int v=sc.nextInt(); Listvl=new ArrayList<>(); for(int i=0;i<v;i++){ vl.add(sc.nextInt()); } Setsl=new HashSet<>(); int res=0; for(Integer i:vl){ if(bl.contains(i)) continue; sl.add(i); } for(Integer i:sl){ String s=i+""; res+=s.length(); } System.out.println(res); } }
def prev(now, l, h, blocked): if now != l: if (now - 1) not in blocked: return (now - 1) else: return prev(now - 1, l, h, blocked) else: if h not in blocked: return h else: return prev(h, l, h, blocked) def next(now, l, h, blocked): if now != h: if (now + 1) not in blocked: return (now + 1) else: return next(now + 1, l, h, blocked) else: if l not in blocked: return l else: return next(l, l, h, blocked) def digits(n): count = 0 while n > 0: n = n // 10 count += 1 return count for i in range(2): if i == 0: l = int(input()) else: h = int(input()) b = int(input()) blocked = [] for i in range(b): blocked.append(int(input())) back = -1 now = -1 c = int(input()) k = 0 for i in range(c): n = int(input()) n1 = digits(n) if now == -1: now = n k += n1 continue if back == n: k += 1 back, now = now, back continue pf = 0 pb = 0 now1 = now prev1 = now for j in range(n1): if j == (n1 - 1): pf = n1 pb = n1 break else: now1 = next(now1, l, h, blocked) pf += 1 prev1 = prev(prev1, l, h, blocked) pb += 1 if now1 == n: break if prev1 == n: break k += pf back = now now = n print(k)
Question 8 : Choco and chocolate
Problem Statement :
Choco, a chocolate lover, has N amount of money with him. He wants to buy as much chocolate as possible. So, he goes to a chocolate shop “Bandyman ”. Mike, the owner of “Bandyman ” has different types of chocolate in his store (represented by a character) placed in a row.
Mike, give an offer to Choco that he can buy a selected type of chocolate for free and need to pay for the other types of chocolates and Choco can only buy consecutive chocolates.
Now, you need to write a code to find the maximum amount of chocolates Choco can get by selecting the chocolates optimally.
Input format :
1st line contains 2 space separated integers A and B denoting the number of chocolates and the amount of money Choco has.
The 2nd line contains A chocolates represented by a string. All chocolates represented by lowercase alphabets.
The 3rd line represents 26 space separated integers representing the cost to buy the chocolates.
[First integer represents the cost of the chocolate of type ‘a’, 2nd integer represents the cost of the chocolates of type ‘b’ and so on]
Output format :
Print the maximum number of chocolates Choco can buy.
Constraints :
1<=A<=10^5
1<=B<=10^9
1<=cost of chocolate<=10^9
Sample input 1 :
6 10
aabcda
5 4 4 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample output 1 :
4
Explanation :
Choco can select the chocolate of type ‘a’ for free and start buying from index 0 and if he buys “aabc” then he has to pay less (0+0+4+4=8) than the total money he has.
This is the maximum number of chocolates he can get in this case.
#include<bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; string s, s1; cin >> s; cin.ignore(); getline(cin, s1); istringstream ss(s1); vector < int > a; vector < char > a1; while (ss) { string w; ss >> w; if (w == "") break; a.push_back(stoi(w)); } map < char, int > mm; for (auto i: s) if (mm[i] == 0) { mm[i]++; a1.push_back(i); } int t = 0; for (auto i: a1) { vector < int > a2; for (auto i1: s) { if (i1 == i) a2.push_back(0); else a2.push_back(a[i1 - 97]); } int l = 0, su = 0; for (int i2 = 0; i2 < n; i2++) { if (l == i2) su += a2[i2]; else su += a2[i2]; if (su > m) { while (su > m && l <= i2) { su -= a2[l]; l++; } } if (i2 - l + 1 > t) t = i2 - l + 1; } } cout << t; }
import java.util.*; class Main { public static int solve(int n, int amnt, String s, int[] price) { int[] freq = new int[26]; char[] ch = s.toCharArray(); int temp = 0, ans = 0, maxFreq = 0, st = 0; for (int i = 0; i < n; i++) { int indx = ch[i] - 'a'; freq[indx] += price[indx]; temp += price[indx]; maxFreq = Math.max(maxFreq, freq[indx]); if (temp - maxFreq > amnt) { while (temp > amnt) { boolean b = false; int tempIndx = ch[st] - 'a'; if (maxFreq == freq[tempIndx]) { b = true; } temp -= price[tempIndx]; freq[tempIndx] -= price[tempIndx]; st++; if (b) { maxFreq = 0; for (int j = 0; j < 26; j++) { maxFreq = Math.max(freq[j], maxFreq); } } if (temp - maxFreq < amnt) { break; } } } ans = Math.max(ans, i - st + 1); } return ans; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int amnt = sc.nextInt(); String s = sc.next(); int[] price = new int[26]; for (int i = 0; i < n; i++) { price[i] = sc.nextInt(); } System.out.println(solve(n, amnt, s, price)); } }
n, m = map(int, input().split()) s = input().strip() a = list(map(int, input().split())) s1 = set() for i in s: if i not in s1: s1.add(i) s1 = list(s1) t = 0 for i in s1: a1 = [] for i1 in s: if i1 == i: a1.append(0) else: a1.append(a[ord(i1) - 97]) l = 0 su = 0 for i2 in range(n): if l == i2: su = a1[i2] else: su += a1[i2] if su > m: while su > m and l <= i2: su -= a1[l] l += 1 if (i2 - l + 1) > t: t = i2 - l + 1 print(t)
Question 9: Formatting large Products
Problem Statement: Rohan is weak in mathematics.He is giving mathematics Olympiad , but he got stuck in one of the question .Help rohan to solve the question.In Question there are two positive integer A and B. You have to find the product of all integer between A and B which is represented in the form C=D*10^E , where C is the product of numbers , D and E are non-negative integers and the last digit of D is non-zero.
Function Description
- Complete the function formatProducts in the editor below, formatProduct must return a string that represents C in the above described form.
- Function has the following parameters
- A: an integer
- B: an integer
Constraints
- A will between 1 and 1,000,000 . Inclusive.
- B will be between A and 1,000,000. Inclusive.
Sample Input 0
- 1
- 5
Sample Output 0
- 12 * 10^1
Explanation
- 1*2*3*4*5=120 = 12 * 10^1
Sample Input 1
- 3
- 10
Sample Output 1
- 18144 * 10^2
Explanation
- 3*4*….*10=1814400 =18144 * 10^2
import java.util.*; public class Main { public static String formatProducts (int a, int b) { int result = 1; for (int i = a; i <= b; i++) result = result * i; int temp = result; int power = 0; while ((result % 10) == 0) { power = power + 1; result = result / 10; } return result + " * 10^" + power; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int a = sc.nextInt (); int b = sc.nextInt (); System.out.println (formatProducts (a, b)); } }
def formatProducts(L,H): a=1 for i in range(L,H+1): a=a*i c=0 while(True): if a%10!=0: break else: c+=1 a=a//10 return (str(a)+" * 10^"+str(c)) L=int(input()) H=int(input()) print(formatProducts(L,H))
#include<bits/stdc++.h> using namespace std; string formatProducts(int a, int b) { int result = 1; for (int i = a; i <= b; i++) result = result * i; int temp = result; int power = 0; while ((result % 10) == 0) { power = power + 1; result = result / 10; } return to_string(result) + " * 10^" + to_string(power); } int main() { int a, b; cin >> a >> b; cout << formatProducts(a, b) << endl; return 0; }
Question 10 : Game Of Clicks [ R->Hard ]
Problem Statement :
Sahil watches TV all day and gets bored. He started playing this dumb game of identifying minimum number of inputs needed to reach a channel. As his cousin, you have to help him, but you live far from his house. So you decide to write a code that will ask Sahil for some inputs and give outputs respectively.
Here are the problems you need to keep in mind :
- There are 13 buttons on his remote: 10 buttons for the numbers (0-9) to form integers denoting respective channel index, “Up channel” button and “ Down channel” button for going i +1th channel and i-1th channel from i respectively, and a “Last viewed” button to see what’s the last channel before it.
- The number buttons allow you to jump directly to a specific channel (Ex: to go to channel 172 by typing 1,7,2).
- If the channel which you are in is ith and that is the max channel index possible, by Up channel, you will reach the first channel possible. Same goes for the down channel button. You can go to the highest channel possible if you go down from the lowest channel possible.
- Sahil can get from one channel to the next in one of the two ways.
- Sahil’s parents have set some parental control on some channels on Aniruth’s television. The “Up Channel “ and “Down buttons” buttons skip these channels as these channels are not viewable.
- Given a list of channels to view, the lowest channel, the highest channel, and a list of blocked channels, your program should return the minimum number of clicks necessary to get through all the shows that Anirudh would like to match.
Input Format :
- First line is the lowest Channel
- Second-line is the highest Channel
- Followed by a number of blocked channels B,
and the next B lines contain the actual blocked channels. - Followed by the number of Channels to view V, and the next V lines contain the actual channels to view.
Constraints :
- The lowest channel on the television will be greater than 0. and less than or equal to 10,000.
- The highest channel on the television will be greater than or equal to the lowest channel. and less than or equal to 10.000.
- The list of channels that are blocked on Anirudh’s television. All the channels in this list will be valid channels (greater than or equal to lowest channel, less than or equal 1 to highest channel). Duplicates may be Ignored. The blocked list can be a maximum of 40 channels.
- The sequence that Sahil must view contains between 1 and 50 elements. inclusive. All channels in this sequence are not in the blocked list and are between lowest channel and highest channel. Inclusive.
Sample Input 0:
1
20
2
18
19
5
15
14
17
11
17
Sample output 0:
8
#include<bits/stdc++.h> using namespace std; unordered_map < int, int > m; int l, u; int util(int a, int b) { if (a == b) return 0; if (m[a]) return util(a + 1, b); return 1 + util(a + 1, b); } int func(int b, int prev) { if (b < prev) return min(util(prev, u) + util(l, b) + 1, util(b, prev)); else return min(util(prev, b), util(l, b) + util(prev, u) + 1); } int main() { int flag = 0, ans = 0, prev, prev2; cin >> l >> u; int bn, b; cin >> bn; while (bn--) { cin >> b; m[b]++; } cin >> bn; while (bn--) { cin >> b; if (b > 9 && flag == 0) { ans += 2; flag++; prev = b; } else if (flag == 0) { ans += 1; flag++; prev = b; } else if (prev2 == b) { prev2 = prev; prev = b; ans++; } else { ans += min(b > 9 ? 2 : 1, func(prev, b)); prev2 = prev; prev = b; } } cout << ans; }
def prev(now, l, h, blocked): if now != l: if (now - 1) not in blocked: return now - 1 else: return prev(now - 1, l, h, blocked) else: if h not in blocked: return h else: return prev(h, l, h, blocked) def next(now, l, h, blocked): if now != h: if (now + 1) not in blocked: return now + 1 else: return next(now + 1, l, h, blocked) else: if l not in blocked: return l else: return next(l, l, h, blocked) def digits(n): count = 0 while n > 0: n = n // 10 count += 1 return count for i in range(2): if i == 0: l = int(input()) else: h = int(input()) b = int(input()) blocked = [] for i in range(b): blocked.append(int(input())) back = -1 now = -1 c = int(input()) k = 0 for i in range(c): n = int(input()) n1 = digits(n) if now == -1: now = n k += n1 continue if back == n: k += 1 back, now = now, back continue pf = 0 pb = 0 now1 = now prev1 = now for j in range(n1): if j == (n1 - 1): pf = n1 pb = n1 break else: now1 = next(now1, l, h, blocked) pf += 1 prev1 = prev(prev1, l, h, blocked) pb += 1 if now1 == n: break if prev1 == n: break k += pf back = now now = n print(k)
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int l = sc.nextInt(); int h = sc.nextInt(); int b = sc.nextInt(); List blackList = new ArrayList<>(); for (int i = 0; i < b; i++) { blackList.add(sc.nextInt()); } int v = sc.nextInt(); Set validList = new HashSet<>(); for (int i = 0; i < v; i++) { int value = sc.nextInt(); if (!blackList.contains(value)) { validList.add(value); } } int totalLength = 0; for (int value : validList) { totalLength += String.valueOf(value).length(); } System.out.println(totalLength); } }
FAQs on Darwin Coding Questions
Question 1: What is the eligibility criteria for Darwinbox Hiring Process?
Darwinbox has set the basic eligibility criteria for hiring freshers:
- Course & Stream : B.Tech – CS/IT
- Batch – 2024
- Score Required: Minimum 6 CGPA or Equivalent % in 10th, 12th & Graduation.
Question 2: How many rounds are there in Darwinbox interview?
There are 2 Rounds of Interviews after clearing both Online Coding Assessments of Darwinbox:
- Technical Interview
- HR Interview
Question 3: Does Darwinbox includes Coding Assessment in their Hiring Process?
Yes, Darwinbox includes Coding Question in their Hiring Process. They generally conduct 2 Round of Coding Assessments.
Question 4: Does Darwinbox hire freshers off campus?
Yes, Darwinbox always conducts Off-Campus drives every year in various Colleges and Universities. For more details get in touch with Training and Placement Officer of your Institution.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others


30+ Companies are Hiring
Get Hiring Updates right in your inbox from PrepInsta