TCS Elevate Wings1 DCA Coding Questions & Solutions 2024 | Python, Java, MCQs & Exam Tips
Problem 1: Parallel Columbus
Problem Statement
A Nobel Prize-winning physicist creates a grid-based race with multiple versions of Christopher Columbus from parallel universes. Columbus explores the grid starting from (1,1)
. However, when Columbus encounters people at cell (x, y)
, he believes it is India and halts exploration.
Given grid dimensions and coordinates of the "American" cell, determine how many different Columbus(s) will encounter people at (n, m)
for the first time.
Function Description:
Complete the `markgame` function with the following parameters:
- n: Number of rows in the grid.
- m: Number of columns in the grid.
- x: Row coordinate of the American cell.
- y: Column coordinate of the American cell.
Constraints:
- 1 ≤ n, m ≤ 100
- 1 ≤ x ≤ n
- 1 ≤ y ≤ m
Sample Case:
Input:
2
2
2
1
Output:
1
Explanation:
The only path is:(1,1) -> (2,1) -> (2,2)
C++ Solution:
#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 totalPaths = (Fact(m + n) / (Fact(m) * Fact(n)));
int blockedPaths = (Fact(x + y) / (Fact(x) * Fact(y))) *
(Fact(m - x + n - y) / (Fact(m - x) * Fact(n - y)));
cout << totalPaths - blockedPaths;
}
Python Solution:
import math
n = int(input()) - 1
m = int(input()) - 1
x = int(input()) - 1
y = int(input()) - 1
total_paths = math.factorial(n + m) // (math.factorial(n) * math.factorial(m))
blocked_paths = (math.factorial(x + y) // (math.factorial(x) * math.factorial(y))) * \
(math.factorial(n - x + m - y) // (math.factorial(n - x) * math.factorial(m - y)))
print(total_paths - blocked_paths)
Problem 2: Amusement Park Discount
Problem Statement
Students visiting an amusement park can earn discounts based on their coding ability. Given a string representing ticket costs and a number (K) (the number of tickets to remove), find the smallest possible ticket cost after removal.
Constraints:
- 1 ≤ number of tickets ≤ 10^5
- 1 ≤ K ≤ number of tickets
Sample Input:
203
3
Sample Output:
0
C++ Solution:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int smallestNumber(string num, int k) {
if (num.length() <= k) return 0;
unordered_map<char, int> pos;
for (int i = 0; i < num.length(); i++) pos[num[i]] = i;
string temp = num;
sort(num.begin(), num.end());
string ans = num.substr(0, num.length() - k);
vector<int> indices;
for (char ch : ans) indices.push_back(pos[ch]);
sort(indices.begin(), indices.end());
string result;
for (int idx : indices) result += temp[idx];
return stoi(result);
}
int main() {
string s;
cin >> s;
int k;
cin >> k;
cout << smallestNumber(s, k) % (int)(pow(10, 9) + 7);
}
Python Solution:
n = input()
k = int(input())
n_len = len(n)
if n_len <= k:
print(0)
else:
result = ""
i = 0
while i < (n_len - 1) and k > 0:
if int(n[i]) > int(n[i + 1]):
k -= 1
i += 1
else:
result += n[i]
i += 1
result += n[i:]
print(int(result[:-k]) if k > 0 else int(result) % (10**9 + 7))
TCS Elevate Wings1 Text Sorting Problem
Given several lines of text, sort them by the number of occurrences of a specific word.
Example:
Input:
3
python
I love python
python is awesome
learning python is fun
Output:
learning python is fun
I love python
python is awesome
Python Solution:
n = int(input())
word = input().lower()
texts = [input().lower() for _ in range(n)]
texts.sort(key=lambda x: x.count(word))
print("\n".join(texts))
Prepare well with these coding questions and solutions to maximize your performance in the TCS Elevate Wings1 coding exam.
Post a comment
Get your FREE PDF on "100 Ways to Try ChatGPT Today"
Generating link, please wait for: 60 seconds
Comments
Join the conversation and share your thoughts! Leave the first comment.