# Thoughtworks Coding Questions And Answers

## Thoughtworks Coding Questions For Freshers

Thoughtworks Coding Questions is a part of Online Test of Thoughtworks which is conducted on Hackerearth Platform. The information on this page will help you to have a bigger picture about the Application Developer Role at Thoughtworks. We recommend you to go through this page thoroughly.

Get to know about Recruitment Process, Online Assessments which take place when you appear for Thoughtworks Application Developer hiring drive. Global technology consulting firm Thoughtworks combines strategy, engineering, and design to promote digital innovation. The clients have relied on their independent teams to develop solutions that go beyond the apparent for more than 28 years.

Here, recent computer science graduates meet with seasoned engineers, self-taught programmers, career changers in their forties, and others to challenge and learn from one another. With the power of their cultivation culture, which has received countless honours worldwide, career journeys bloom.

### Recruitment Process at Thoughtworks

Below is a description of Thoughtworks hiring procedure that will aid you in understanding the method used to find applicants for the position of Application Developer – Graduate Consultant.

The Thoughtworks Recruitment Process contains the mentioned rounds.

• Pre – Placement Talk
• Online Test
• Code Pairing Round
• Technical Interview
• Leadership & Social Change Interview

ThoughtworksRelated Information
Batch2023
RoleB.Tech (CSE IT ECE)
Education
• 70% aggregate / 7.4 CGPA in Xth, XIIth & Engineering
• No backlogs
PlatformHackerearth
Cost to Company (CTC)11.10 LPA
ExpertiseDS,  Algorithms, OS,  Networking, Java, OOPs Concept

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Sample Thoughtworks Coding Questions With Solutions

### Question 1 : Number with 2

Problem Statement  :

Suppose you are in a number system, where if the number doesn’t contain 2 in the unit digit then the number is not valid. So the first number of the number system is 2, the second number is 12, and the third is 22.
for a given integer n, you have to print the nth element of the number system.

Input Format:
First line, containing n denoting the number of test cases.
then n number of lines for the query.

Output Format:
Print the consecutive number in the number system for each query.

Sample Input:
3

Sample Output:
22

Explanation:
1st number will be 2 , 2nd number will be 12 and third number will be 32

Run
```#include

using namespace std;

int main() {
int n;
cin >> n;
cout << (n - 1) * 10 + 2;

return 0;
}
```
Run
```import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
System.out.println((n - 1) * 10 + 2);
}
}```
Run
```n = int(input())
print((n - 1) * 10 + 2)
```

### Question 2 : Duplicates

Problem Statement  :

The principal has a problem with repetitions. Everytime someone sends the same email twice he becomes angry and starts yelling. His personal assistant filters the mails so that all the unique mails are sent only once, and if there is someone sending the same mail again and again, he deletes them. Write a program which will see the list of roll numbers of the student and find how many emails are to be deleted.

Sample Input:
6
1
3
3
4
3
3

Sample Output:
3

Run
```#include <bits/stdc++.h>

using namespace std;
map < int, int > m;
int main() {
int n, a, ans = 0;
cin >> n;
while (n--) {
cin >> a;
m[a]++;
if (m[a] > 1) ans++;
}
cout << ans;
}
```
Run
```import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
TreeSet < Integer > list = new TreeSet < > ();
for (int i = 0; i < n; i++)
System.out.println(Math.abs(n - list.size()));
}
}```
Run
```def countDuplicate(numbers):
c = 0
for i in set(numbers):
if numbers.count(i) > 1:
c += numbers.count(i) - 1
return c

n = int(input())
numbers = []
for i in range(n):
numbers.append(int(input()))
print(countDuplicate(numbers))
```

### Question 3 : Maximum Revenue

Problem statement : Amit is a salesman who wishes to know the maximum revenue received from a given item of the N products each day . Amit has a sales record of N products for M days.Helo amit to find the highest revenue received each day.

#### Input :

• The first line of the input consists of two space-separated integers- day(M) and item(N) representing the days and the products in the sales record.
• The next M lines consist of N space separated integers representing the sales revenue from each product each day.

#### Output:

• Print m space-separated integers representing the maximum revenue received each day .

#### Example Input:

• 3 4
• 101 123 234 344
• 143  282 499 493
• 283 493 202 304

• 344 499 493

#### Explanation:

• The maximum revenue received on the first day is 344 , followed by a maximum revenue of 499 on the second day and a maximum revenue of 493 on the third day.
Run
```#include <bits/stdc++.h>
using namespace std;

int main(){
int m,n, i,j;
cin >> m;
cin >> n;
int arr [m][n];
for(i = 0;i < m;i++){
for (j=0;j<n;j++){ cin >> arr[i][j];
}
}
for (i = 0;i < m;i++){
int max = INT_MIN;
for(j=0;j<n;j++){ if(arr[i][j]>max){
max = arr[i][j];
}
}
cout  << max << " ";
}
return 0;
}
```
Run
```import java.util.*;

public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int arr[][]=new int[m][n];
int i,j;
for(i=0;i<m;i++){
for (j=0;j<n;j++){
arr[i][j]=sc.nextInt();
}
}
for (i=0;i<m;i++){
int max = Integer.MIN_VALUE;
for(j=0;j<n;j++){ if(arr[i][j]>max){
max = arr[i][j];
}
}
System.out.println(max);
}
}
}
```
Tab Content
Run
```m = int(input())
n = int(input())
arr = []
for i in range(0, m):
ar = []
for j in range(0, n):
ar.append(int(input()))
arr.append(ar)

for i in arr:
print(max(i), end=" ")
```

### Question 4 : 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
1T
17
Sample output 0:
7

Run
```#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;
}
```
Run
```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 < Integer > bl = new ArrayList < > ();
for (int i = 0; i < b; i++) {
}
int v = sc.nextInt();
List < Integer > vl = new ArrayList < > ();
for (int i = 0; i < v; i++) {
}
Set < Integer > sl = new HashSet < > ();
int res = 0;
for (Integer i: vl) {
if (bl.contains(i))
continue;
}
for (Integer i: sl) {
String s = i + "";
res += s.length();
}
System.out.println(res);

}
}```
Run
```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 5 : Share Holder (R -> Hard)

Problem statement :

Ratan is a crazy rich person. And he is blessed with luck, so he always made the best profit possible with the shares he bought. That means he bought a share at a low price and sold it at a high price to maximize his profit. Now you are an income tax officer and you need to calculate the profit he made with the given values of stock prices each day. You have to calculate only the maximum profit Ratan earned.
Note that:
Ratan never goes into loss.

Example 1 :

Price=[1,6,2]
Ratan buys it on the first day and sells it on the second.

Example 2 :

Price=[9,8,6]

The Price always went down, Ratan never bought it.

Input Format:
First line with an integer n, denoting the number days with the value of the stack
Next n days, telling the price of the stock on that very day.

Output Format:
Maximum profit done by Ratan in a single line.
Constraints:
Number of days <=10^8

Sample Input for Custom Testing

STDIN
———–
7
1
9
2
11
1
9
2

Sample Output

10

Explanation

The maximum profit possible is when Ratan buys it in 1 rupees and sells it in 11.

Run
```#include <bits/stdc++.h>

using namespace std;
int solve(vector < int > v) {
int n = v.size();
if (n == 0)
return 0;
int mx = v;
for (int i = 1; i < n; i++)
mx = max(mx, v[i]);
if (mx <= 0)
return 0;
int mxSum = 0;
int cSum = 0;
for (int i = 0; i < n; i++) {
cSum += v[i];
if (cSum < 0) cSum = 0; mxSum = max(mxSum, cSum); } return mxSum; } int main() { int n; cin >> n;
int price[n];
for (int i = 0; i < n; i++) cin >> price[i];
vector < int > diff;
for (int i = n - 2; i >= 0; i--) diff.push_back(price[i + 1] - price[i]);
int ans = solve(diff);
if (ans < 0) cout << 0 << endl;
else cout << ans << endl;
}
```
Run
```import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int price[] = new int[n];
for (int i = 0; i < n; i++) {
price[i] = sc.nextInt();
}
Vector < Integer > diff = new Vector < > ();
for (int i = n - 2; i >= 0; i--) {
}
int ans = solve(diff);
if (ans < 0) {
System.out.println(0);
} else {
System.out.println(ans);
}

}
private static int solve(Vector < Integer > v) {
int n = v.size();
if (n == 0) {
return 0;
}
int mx = v.get(0);
for (int i = 1; i < n; i++) {
mx = Math.max(mx, v.get(i));
}
if (mx <= 0) {
return 0;
}
int mxSum = 0, csum = 0;
for (int i = 0; i < n; i++) {
csum += v.get(i);
if (csum < 0)
csum = 0;
mxSum = Math.max(csum, mxSum);
}
return mxSum;
}
}
```
Run
```def func(diff):
n=len(diff)
if n==0:
return 0
mx=max(diff)
if mx <= 0:
return 0
mxS=0
cS=0
for i in diff:
cS+=i
if cS <= 0:
cS=0
mxS=max(cS,mxS)
return mxS
n=int(input())
arr=[]
diff=[]
ans=
for i in range(n):
arr.append(int(input()))
for i in range(n-1):
diff.append(arr[i+1]-arr[i])
ans=func(diff)
if ans < 0:
print("0")
else:
print(ans)```

## FAQs related to Thoughtworks Coding Questions

##### Question 1: How the Recruitment test is conducted by Thoughtworks?

Thoughtworks has partnered with Hackerearth for conducting the Online Coding Assessments for Recruitment Process.

##### Question 2: What are the major field does Thoughtworks works on?

ThoughtWorks works on a wide variety of projects across industries, including e-commerce, healthcare, finance, and education. Some examples of past projects include developing a mobile banking app, creating a platform for healthcare data analysis, and building a system to optimize supply chain operations.

## 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