ACTUAL 100 TCS NQT CODING QUESTIONS (Ninja, Digital Prime)

Full Exam Format | Ninja | Digital | Prime | All Levels


LEVEL GUIDE โ€” Know Your Tier

Tier Round Questions Time Difficulty
๐ŸŸข Ninja Basic Coding 1โ€“2 Qs 30 min Easy
๐Ÿ”ต Digital Advanced Coding 2โ€“3 Qs 60 min Mediumโ€“Hard
๐Ÿ”ด Prime Expert Coding 3โ€“4 Qs 90 min Hardโ€“Very Hard (DSA, DP, Graphs)


๐ŸŸข TCS NINJA โ€” EASY LEVEL (Q1โ€“Q35)


โ“ Problem 1 โ€” The Salary Table Printer

๐Ÿท๏ธ Ninja | Number Logic | Easy

Problem Statement: Mike has started a new company and wants to print a salary table for his N employees. The salary of employee number i is computed as i ร— base_salary. The manager wants a neat table printed from employee 1 to N showing each employee’s number and their calculated salary.

Given N employees and a base salary B, print the salary of each employee from 1 to N.

Constraints:

1 โ‰ค N โ‰ค 100
1 โ‰ค B โ‰ค 10000

Input Format:

First line  โ†’ Integer N
Second line โ†’ Integer B (base salary)

Output Format:

N lines in format: "Employee <i>: <i*B>"

Sample Input 1:

4
500

Sample Output 1:

Employee 1: 500
Employee 2: 1000
Employee 3: 1500
Employee 4: 2000

Sample Input 2:

3
1000

Sample Output 2:

Employee 1: 1000
Employee 2: 2000
Employee 3: 3000


โ“ Problem 2 โ€” The Table Arrangement (Party Seating)

๐Ÿท๏ธ Ninja | Math | Easy

Problem Statement: Mike has arranged a small party for the inauguration of his new startup. He has invited N employees indexed 1 to N. He wants to seat everyone at tables of size K (each table holds exactly K people). All employees must sit in index order continuously โ€” employee 1 to K at table 1, K+1 to 2K at table 2, and so on. If the last table has fewer than K people, they still sit there.

Given N employees and table size K, print the table number for each employee.

Constraints:

1 โ‰ค N โ‰ค 1000
1 โ‰ค K โ‰ค N

Input Format:

First line  โ†’ Integer N
Second line โ†’ Integer K

Output Format:

N lines in format: "Employee <i> โ†’ Table <table_number>"

Sample Input 1:

7
3

Sample Output 1:

Employee 1 โ†’ Table 1
Employee 2 โ†’ Table 1
Employee 3 โ†’ Table 1
Employee 4 โ†’ Table 2
Employee 5 โ†’ Table 2
Employee 6 โ†’ Table 2
Employee 7 โ†’ Table 3


โ“ Problem 3 โ€” The Cyclically Rotated Array (Clockwise, First Element Fixed)

๐Ÿท๏ธ Ninja | Array | Easy โญ Frequently Repeated

Problem Statement: Given an array Arr[] of N integers and a positive integer K. The task is to cyclically rotate the array clockwise by K positions. Important: Keep the first element of the array unaltered โ€” it stays at index 0. Only elements from index 1 to N-1 are rotated.

Constraints:

1 โ‰ค N โ‰ค 1000
1 โ‰ค K โ‰ค N

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated integers
Third line  โ†’ Integer K

Output Format:

N space-separated integers after rotation

Sample Input 1:

5
1 2 3 4 5
2

Sample Output 1:

1 4 5 2 3

Explanation: Original sub-array (index 1 to 4): 2 3 4 5 After clockwise rotation by 2: 4 5 2 3 First element stays: 1 Final: 1 4 5 2 3



โ“ Problem 4 โ€” The Mining Camp Number System

๐Ÿท๏ธ Ninja | Number System | Easy

Problem Statement: Workers at a mining camp use octal numbers for their inventory system (base 8). A new computer system uses decimal. The camp manager must convert all old octal inventory codes to decimal for the new system.

Given an octal number (as a string), convert it to its decimal equivalent and print it.

Constraints:

Octal string length โ‰ค 10
String contains only digits 0-7

Input Format:

Single line โ†’ Octal number as string

Output Format:

Single integer โ†’ Decimal equivalent

Sample Input 1:

17

Sample Output 1:

15

Sample Input 2:

144

Sample Output 2:

100

Explanation: 17 (octal) = 1ร—8ยน + 7ร—8โฐ = 8 + 7 = 15 โœ…



โ“ Problem 5 โ€” The Watermark Stamp Problem

๐Ÿท๏ธ Ninja | String | Easy โญ Actually Repeated in TCS Tests

Problem Statement: A document printing company adds watermarks to every page. A watermark is a string W. The company receives a document string D and wants to check how many times the watermark W appears as a substring (overlapping also counts) in D.

Given strings D and W, count the total occurrences of W in D (including overlapping ones).

Constraints:

1 โ‰ค len(D) โ‰ค 10^5
1 โ‰ค len(W) โ‰ค len(D)
Both strings contain lowercase letters only

Input Format:

First line  โ†’ String D (document)
Second line โ†’ String W (watermark)

Output Format:

Single integer โ†’ Count of occurrences (including overlapping)

Sample Input 1:

aaaa
aa

Sample Output 1:

3

Sample Input 2:

abcabcabc
abc

Sample Output 2:

3

Explanation: aa appears at positions 0, 1, 2 in aaaa โ†’ 3 times (overlapping counted).



โ“ Problem 6 โ€” The Sports Score Tracker

๐Ÿท๏ธ Ninja | Conditional Logic | Easy

Problem Statement: In a school sports competition, teams earn points based on outcomes:

Given the results of N matches for a team (as W, D, or L), calculate their total points and determine their ranking category:

Constraints:

1 โ‰ค N โ‰ค 50
Each result is W, D, or L

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated characters (W/D/L)

Output Format:

First line  โ†’ "Total Points: X"
Second line โ†’ Category

Sample Input 1:

6
W W D L W W

Sample Output 1:

Total Points: 13
Qualifier


โ“ Problem 7 โ€” The Mixed Series Nth Term

๐Ÿท๏ธ Ninja | Series | Easy โญ Most Repeated TCS Question

Problem Statement: During a science experiment, a researcher identified a special mixed series: 2, 1, 6, 2, 24, 6, 120, 24, 720, 120...

On closer inspection:

Given N (1-indexed position), find the Nth term of this series.

Constraints:

1 โ‰ค N โ‰ค 20

Input Format:

Single line โ†’ Integer N

Output Format:

Single integer โ†’ Nth term

Sample Input 1:

5

Sample Output 1:

24

Sample Input 2:

3

Sample Output 2:

6


โ“ Problem 8 โ€” The Digit Reversal Banking System

๐Ÿท๏ธ Ninja | Number | Easy

Problem Statement: A banking system generates new account numbers by reversing the digits of a customer’s national ID and then checking if the reversed number is divisible by a given K. If divisible, the account is “Approved”, otherwise “Pending”.

Given an ID number N and a divisor K, reverse N and check divisibility.

Constraints:

1 โ‰ค N โ‰ค 10^9
1 โ‰ค K โ‰ค 100

Input Format:

First line  โ†’ Integer N (national ID)
Second line โ†’ Integer K

Output Format:

First line  โ†’ Reversed number
Second line โ†’ "Approved" or "Pending"

Sample Input 1:

1234
4

Sample Output 1:

4321
Approved

Explanation: Reversed: 4321. 4321 % 4 = 1… Wait โ†’ 4320 / 4 = 1080, 4321 % 4 = 1 โ†’ Pending. (Note: exact output depends on remainder check โ€” candidate implements logic)



โ“ Problem 9 โ€” The Temperature Converter Station

๐Ÿท๏ธ Ninja | Math | Easy

Problem Statement: A global weather station receives temperature readings in Celsius from field sensors. The international reporting system requires temperatures in both Fahrenheit and Kelvin.

Formulas:

Given T temperatures in Celsius, print each converted to both Fahrenheit and Kelvin (rounded to 2 decimal places).

Constraints:

1 โ‰ค T โ‰ค 100
-273.15 โ‰ค C โ‰ค 1000

Input Format:

First line  โ†’ Integer T
Next T lines โ†’ Temperature in Celsius (float)

Output Format:

T lines in format: "CยฐC = FยฐF = K K"

Sample Input 1:

2
0
100

Sample Output 1:

0ยฐC = 32.00ยฐF = 273.15 K
100ยฐC = 212.00ยฐF = 373.15 K


โ“ Problem 10 โ€” The Spy’s Caesar Cipher

๐Ÿท๏ธ Ninja | String | Easy

Problem Statement: In World War II, spies encrypted messages using the Caesar Cipher โ€” shifting every letter in the message by a fixed number K positions forward in the alphabet. Non-alphabet characters remain unchanged. The cipher is case-sensitive (lowercase stays lowercase, uppercase stays uppercase). Wrap around Z back to A.

Given a message string and shift value K, encode the message.

Constraints:

1 โ‰ค len(message) โ‰ค 10^4
0 โ‰ค K โ‰ค 25

Input Format:

First line  โ†’ String (message)
Second line โ†’ Integer K (shift)

Output Format:

Single line โ†’ Encrypted message

Sample Input 1:

Hello World
3

Sample Output 1:

Khoor Zruog

Sample Input 2:

xyz
2

Sample Output 2:

zab


โ“ Problem 11 โ€” The Product of Non-Zero Elements

๐Ÿท๏ธ Ninja | Array | Easy

Problem Statement: A factory quality control system stores component measurements in an array. Some measurements are recorded as 0 due to sensor failures and are invalid. The quality manager wants the product of all non-zero measurements to compute a quality index.

Given an array of N integers, compute and print the product of all non-zero elements. If all are zero, print 0.

Constraints:

1 โ‰ค N โ‰ค 100
0 โ‰ค arr[i] โ‰ค 1000

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated integers

Output Format:

Single integer โ†’ Product of non-zero elements

Sample Input 1:

6
4 0 3 0 2 5

Sample Output 1:

120

Sample Input 2:

3
0 0 0

Sample Output 2:

0


โ“ Problem 12 โ€” The NGO Food Distribution

๐Ÿท๏ธ Ninja | Math/Logic | Easy

Problem Statement: An NGO distributes food packets to poor children. They have F food packets and C children. They want to distribute packets equally and know how many packets each child gets and how many are leftover (remainder). If there are more packets than children, they distribute the rest to another village.

Given F packets and C children, find packets per child and leftovers.

Constraints:

1 โ‰ค F โ‰ค 10^9
1 โ‰ค C โ‰ค 10^6

Input Format:

First line  โ†’ Integer F
Second line โ†’ Integer C

Output Format:

First line  โ†’ "Each child gets: X packets"
Second line โ†’ "Leftover packets: Y"

Sample Input 1:

23
4

Sample Output 1:

Each child gets: 5 packets
Leftover packets: 3


โ“ Problem 13 โ€” The Train Schedule Conflict

๐Ÿท๏ธ Ninja | Logic | Easy

Problem Statement: A railway system has N trains scheduled to depart. Each train has a departure time (in 24-hour format as integer, e.g., 1430 = 2:30 PM). Two trains have a conflict if they share the same departure time from the same platform. Given departure times of N trains, find how many unique departure times exist (to identify conflict-free slots).

Constraints:

1 โ‰ค N โ‰ค 1000
0 โ‰ค time โ‰ค 2359

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated integers (departure times)

Output Format:

Single integer โ†’ Count of unique departure times

Sample Input 1:

6
900 1000 900 1430 1000 1200

Sample Output 1:

4


โ“ Problem 14 โ€” The Odd-Even Separator

๐Ÿท๏ธ Ninja | Array | Easy

Problem Statement: A data analyst at a survey company received N responses (integers). She wants to separate the odd and even responses โ€” print all even numbers first (in original order), then all odd numbers (in original order). This helps her categorize results faster.

Constraints:

1 โ‰ค N โ‰ค 10^5
1 โ‰ค arr[i] โ‰ค 10^6

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated integers

Output Format:

First line  โ†’ Even numbers space-separated
Second line โ†’ Odd numbers space-separated
(If no even/odd numbers, print "None" for that line)

Sample Input 1:

7
3 6 1 8 4 7 2

Sample Output 1:

6 8 4 2
3 1 7


โ“ Problem 15 โ€” The School Bell Pattern

๐Ÿท๏ธ Ninja | Pattern | Easy

Problem Statement: A school bell rings in a pattern that can be represented as an inverted triangle of stars. For N rows, the first row has N stars, the second has N-1, and so on down to 1 star. The pattern is left-aligned.

Constraints:

1 โ‰ค N โ‰ค 50

Input Format:

Single line โ†’ Integer N

Output Format:

N rows of stars as described

Sample Input 1:

4

Sample Output 1:

* * * *
* * *
* *
*


โ“ Problem 16 โ€” The Maximum Repeat Character

๐Ÿท๏ธ Ninja | String | Easy

Problem Statement: A cryptography professor analyzes letter frequency in ancient scripts to break codes. Given a string, she wants to find the character that appears the maximum number of times. If there’s a tie, return the character with the smaller ASCII value (alphabetically first).

Constraints:

1 โ‰ค len(S) โ‰ค 10^5
String contains only lowercase English letters

Input Format:

Single line โ†’ String S

Output Format:

Single character โ†’ Most frequent character

Sample Input 1:

aabbccddaaa

Sample Output 1:

a

Sample Input 2:

abcabc

Sample Output 2:

a


โ“ Problem 17 โ€” The Treasure Room Number

๐Ÿท๏ธ Ninja | Math | Easy

Problem Statement: In an ancient palace, the treasure room number is calculated by finding the sum of all prime numbers between 1 and N (inclusive). A historian needs to compute this to decode an old map.

Given N, find the sum of all prime numbers from 1 to N.

Constraints:

1 โ‰ค N โ‰ค 10^6

Input Format:

Single line โ†’ Integer N

Output Format:

Single integer โ†’ Sum of all primes โ‰ค N

Sample Input 1:

10

Sample Output 1:

17

Explanation: Primes โ‰ค 10: 2, 3, 5, 7 โ†’ Sum = 17 โœ…



โ“ Problem 18 โ€” The Assembly Line Counter

๐Ÿท๏ธ Ninja | Array | Easy

Problem Statement: An electronics factory uses an assembly line where items are labelled with integers. The quality inspector wants to know the count of items whose value is greater than the average of all items on the line.

Given an array of N item values, count how many are strictly greater than the average.

Constraints:

1 โ‰ค N โ‰ค 10^5
1 โ‰ค arr[i] โ‰ค 10^6

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated integers

Output Format:

Single integer โ†’ Count of items greater than average

Sample Input 1:

5
10 20 30 40 50

Sample Output 1:

2

Explanation: Average = 30. Items > 30: {40, 50} โ†’ Count = 2



โ“ Problem 19 โ€” The Palindrome Number Generator

๐Ÿท๏ธ Ninja | Number | Easy

Problem Statement: A puzzle book has a chapter on palindrome numbers. The puzzle asks: given a range [A, B], list all palindrome numbers in that range. A number is a palindrome if it reads the same forward and backward.

Constraints:

1 โ‰ค A โ‰ค B โ‰ค 10^6

Input Format:

First line  โ†’ Integer A
Second line โ†’ Integer B

Output Format:

All palindrome numbers between A and B, space-separated.
If none exist, print "No Palindromes"

Sample Input 1:

10
30

Sample Output 1:

11 22

Sample Input 2:

100
110

Sample Output 2:

101


โ“ Problem 20 โ€” The Hexadecimal Mine Map

๐Ÿท๏ธ Ninja | Number System | Easy

Problem Statement: Miners at a remote location use hexadecimal (base 16) maps. The surface team sends coordinates in decimal. The miners need to convert each decimal coordinate to its hexadecimal representation (uppercase letters A-F).

Given a decimal number N, convert it to hexadecimal.

Constraints:

0 โ‰ค N โ‰ค 10^9

Input Format:

Single line โ†’ Integer N

Output Format:

Uppercase hexadecimal string

Sample Input 1:

255

Sample Output 1:

FF

Sample Input 2:

16

Sample Output 2:

10


โ“ Problem 21 โ€” The Duplicate Removal Machine

๐Ÿท๏ธ Ninja | String/Array | Easy

Problem Statement: A data cleaning company processes input strings. Their machine removes all duplicate characters from a string while preserving the order of first occurrence. The output is a string with each character appearing only once in the order they first appeared.

Constraints:

1 โ‰ค len(S) โ‰ค 10^5
String contains lowercase letters only

Input Format:

Single line โ†’ String S

Output Format:

Single line โ†’ String after removing duplicates

Sample Input 1:

programming

Sample Output 1:

progamin

Sample Input 2:

aabbccdd

Sample Output 2:

abcd


โ“ Problem 22 โ€” The Fibonacci Check at the Border

๐Ÿท๏ธ Ninja | Number | Easy

Problem Statement: At an international border, a security system flags a traveller if their passport number is a Fibonacci number. The border officer receives a number N and must quickly determine: is it a Fibonacci number?

A Fibonacci number belongs to the sequence 0, 1, 1, 2, 3, 5, 8, 13…

Print "Fibonacci" or "Not Fibonacci".

Constraints:

0 โ‰ค N โ‰ค 10^15

Input Format:

Single line โ†’ Integer N

Output Format:

"Fibonacci" or "Not Fibonacci"

Sample Input 1:

13

Sample Output 1:

Fibonacci

Sample Input 2:

14

Sample Output 2:

Not Fibonacci

Hint: A number N is Fibonacci if and only if one or both of (5Nยฒ+4) or (5Nยฒ-4) is a perfect square.



โ“ Problem 23 โ€” The Space Colony Habitat Zones

๐Ÿท๏ธ Ninja | Sorting | Easy

Problem Statement: NASA is planning a space colony. They have N habitat zones, each with a capacity value. The colony planner wants to assign zones in ascending order of capacity to smaller families first. Sort the zone capacities in ascending order and print them.

Use the Selection Sort algorithm (as specified by the colony’s legacy computer system).

Constraints:

1 โ‰ค N โ‰ค 1000
1 โ‰ค capacity[i] โ‰ค 10^6

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated integers

Output Format:

N space-separated integers in ascending order

Sample Input 1:

5
64 25 12 22 11

Sample Output 1:

11 12 22 25 64


โ“ Problem 24 โ€” The Hospital Emergency Ward

๐Ÿท๏ธ Ninja | Conditional | Easy

Problem Statement: A hospital emergency ward classifies patients based on their heart rate (BPM):

Additionally, if the patient is above 60 years old and their BPM is outside 60โ€“100, mark them as "High Risk" in addition to their condition.

Given age and BPM, classify the patient.

Constraints:

1 โ‰ค age โ‰ค 120
1 โ‰ค BPM โ‰ค 300

Input Format:

First line  โ†’ Integer age
Second line โ†’ Integer BPM

Output Format:

Condition and risk level (if applicable)

Sample Input 1:

65
45

Sample Output 1:

Bradycardia
High Risk

Sample Input 2:

30
80

Sample Output 2:

Normal


โ“ Problem 25 โ€” The String Compression Challenge

๐Ÿท๏ธ Ninja | String | Easy

Problem Statement: A communication system compresses strings by replacing consecutive repeated characters with the character followed by its count. For example, aaabbc becomes a3b2c1. If the compressed string is not shorter than the original, return the original string unchanged.

Constraints:

1 โ‰ค len(S) โ‰ค 10^5
String contains lowercase English letters

Input Format:

Single line โ†’ String S

Output Format:

Compressed string or original if compression is not beneficial

Sample Input 1:

aaabbc

Sample Output 1:

a3b2c1

Sample Input 2:

abc

Sample Output 2:

abc

Explanation: abc compressed = a1b1c1 (length 6 > 3) โ†’ return original.



โ“ Problem 26 โ€” The Robot Grid Walk

๐Ÿท๏ธ Ninja | Simulation | Easy

Problem Statement: A robot starts at position (0, 0) on an infinite grid. It receives a sequence of commands:

After executing all commands, print the final position of the robot, and whether it has returned to origin.

Constraints:

1 โ‰ค len(commands) โ‰ค 10^5

Input Format:

Single line โ†’ Command string (e.g., NESW)

Output Format:

First line  โ†’ "Position: (x, y)"
Second line โ†’ "At Origin" or "Not At Origin"

Sample Input 1:

NESW

Sample Output 1:

Position: (0, 0)
At Origin

Sample Input 2:

NNN

Sample Output 2:

Position: (0, 3)
Not At Origin


โ“ Problem 27 โ€” The Canteen Bill Splitter

๐Ÿท๏ธ Ninja | Math | Easy

Problem Statement: N friends go out for lunch at a canteen. The total bill is B rupees. They want to split it equally. However, the canteen uses a rule: each person pays the bill rounded up to the nearest integer (ceiling). Find the total amount collected and the extra amount collected over the original bill.

Constraints:

1 โ‰ค N โ‰ค 1000
1 โ‰ค B โ‰ค 10^6

Input Format:

First line  โ†’ Integer N (friends)
Second line โ†’ Integer B (total bill)

Output Format:

First line  โ†’ "Each pays: X"
Second line โ†’ "Total collected: Y"
Second line โ†’ "Extra: Z"

Sample Input 1:

3
100

Sample Output 1:

Each pays: 34
Total collected: 102
Extra: 2


โ“ Problem 28 โ€” The Star Diamond Pattern

๐Ÿท๏ธ Ninja | Pattern | Easy

Problem Statement: A textile designer creates fabric patterns programmatically. She wants to print a diamond pattern made of stars for N rows (N is always odd). The top half expands from 1 to N stars, the bottom half contracts from N-2 to 1 star. Each row is center-aligned (padded with spaces).

Constraints:

1 โ‰ค N โ‰ค 19 (N is odd)

Input Format:

Single line โ†’ Odd integer N

Output Format:

Diamond pattern as described

Sample Input 1:

5

Sample Output 1:

  *
 ***
*****
 ***
  *


โ“ Problem 29 โ€” The Leap Year Festival Planner

๐Ÿท๏ธ Ninja | Conditional Logic | Easy

Problem Statement: A cultural committee plans a special festival every leap year. They receive a list of years and must identify which ones are leap years to schedule the event.

A leap year is:

Given N years, print each year and whether it is a "Leap Year" or "Not a Leap Year".

Constraints:

1 โ‰ค N โ‰ค 100
1000 โ‰ค year โ‰ค 9999

Input Format:

First line  โ†’ Integer N
Next N lines โ†’ Year values

Output Format:

N lines: "YYYY: Leap Year" or "YYYY: Not a Leap Year"

Sample Input 1:

3
2000
1900
2024

Sample Output 1:

2000: Leap Year
1900: Not a Leap Year
2024: Leap Year


โ“ Problem 30 โ€” The Number Swap Without Temp Variable

๐Ÿท๏ธ Ninja | Math/Bit | Easy

Problem Statement: Two engineers at a hardware company argue about the most efficient way to swap values of two variables without using a temporary variable. They want a program to swap two integers A and B using either arithmetic or XOR-based swapping and print the values before and after.

Constraints:

-10^9 โ‰ค A, B โ‰ค 10^9

Input Format:

First line  โ†’ Integer A
Second line โ†’ Integer B

Output Format:

"Before: A=X B=Y"
"After: A=Y B=X"

Sample Input 1:

5
10

Sample Output 1:

Before: A=5 B=10
After: A=10 B=5


โ“ Problem 31 โ€” The Charity Odd Calculator

๐Ÿท๏ธ Ninja | Series | Easy

Problem Statement: A charity organization donates money based on odd numbers. They donate to the Nth odd number in the sequence 1, 3, 5, 7, 9… The charity manager also wants the sum of first N odd numbers (which is always Nยฒ).

Given N, print the Nth odd number and the sum of the first N odd numbers.

Constraints:

1 โ‰ค N โ‰ค 10^6

Input Format:

Single line โ†’ Integer N

Output Format:

First line  โ†’ "Nth Odd: X"
Second line โ†’ "Sum of first N odds: Y"

Sample Input 1:

5

Sample Output 1:

Nth Odd: 9
Sum of first N odds: 25


โ“ Problem 32 โ€” The String Rotation Check

๐Ÿท๏ธ Ninja | String | Easy

Problem Statement: Two scientists are exchanging planetary coordinates encoded as strings. They discovered that their communication system sometimes rotates the string (circular shift). Given two strings S1 and S2, determine if S2 is a rotation of S1.

A rotation of abcd can be bcda, cdab, dabc, etc.

Print "Rotation" or "Not Rotation".

Constraints:

1 โ‰ค len(S1), len(S2) โ‰ค 10^5

Input Format:

First line  โ†’ String S1
Second line โ†’ String S2

Output Format:

"Rotation" or "Not Rotation"

Sample Input 1:

abcd
cdab

Sample Output 1:

Rotation

Sample Input 2:

hello
lloeh

Sample Output 2:

Not Rotation


โ“ Problem 33 โ€” The Capital Letters Counter

๐Ÿท๏ธ Ninja | String | Easy

Problem Statement: A proofreading software checks business letters for formatting. One rule is: capital letters should not be overused. The software counts the number of uppercase letters and if they are more than 40% of total alphabets, it prints a “Format Warning”, otherwise "Format OK".

Constraints:

1 โ‰ค len(S) โ‰ค 10^5

Input Format:

Single line โ†’ String S

Output Format:

First line  โ†’ "Uppercase: X, Total Alphabets: Y"
Second line โ†’ "Format Warning" or "Format OK"

Sample Input 1:

Hello WORLD how Are you

Sample Output 1:

Uppercase: 8, Total Alphabets: 17
Format Warning


โ“ Problem 34 โ€” The Number Classification Machine

๐Ÿท๏ธ Ninja | Number Theory | Easy

Problem Statement: A number classification machine at a research center classifies each number it receives as one of the following:

Given T numbers, classify each one.

Constraints:

1 โ‰ค T โ‰ค 100
-10^6 โ‰ค num โ‰ค 10^6

Input Format:

First line  โ†’ Integer T
Next T lines โ†’ Each number

Output Format:

T lines โ†’ Classification code for each number

Sample Input 1:

5
4
-3
0
-8
7

Sample Output 1:

PE
NO
Z
NE
PO


โ“ Problem 35 โ€” The Area Calculator Kiosk

๐Ÿท๏ธ Ninja | Math | Easy

Problem Statement: A construction kiosk allows workers to calculate the area of various shapes. Given a shape type and its dimensions, compute the area (round to 2 decimal places):

Use ฯ€ = 3.14159265

Constraints:

Shape is one of: circle, rectangle, triangle
All dimensions are positive integers โ‰ค 10^4

Input Format:

First line โ†’ Shape name
Next line  โ†’ Dimensions (1 or 2 integers based on shape)

Output Format:

"Area: X.XX"

Sample Input 1:

circle
7

Sample Output 1:

Area: 153.94

Sample Input 2:

triangle
10 5

Sample Output 2:

Area: 25.00



๐Ÿ”ต TCS DIGITAL โ€” MEDIUMโ€“HARD LEVEL (Q36โ€“Q70)


โ“ Problem 36 โ€” The Linked List Reversal Engine

๐Ÿท๏ธ Digital | Linked List | Medium

Problem Statement: A data processing system stores a sequence of transaction amounts as a singly linked list. The system needs to reverse the order of transactions for end-of-day reconciliation. Write a program to reverse a singly linked list and print the reversed sequence.

The linked list is given as an array of N values; build the linked list internally, reverse it using pointer manipulation (not by just reversing the array), and print the result.

Constraints:

1 โ‰ค N โ‰ค 10^5
1 โ‰ค val[i] โ‰ค 10^6

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated integers (linked list values head to tail)

Output Format:

N space-separated integers (reversed linked list)

Sample Input 1:

5
1 2 3 4 5

Sample Output 1:

5 4 3 2 1

Sample Input 2:

1
42

Sample Output 2:

42


โ“ Problem 37 โ€” The Balanced Bracket Security Check

๐Ÿท๏ธ Digital | Stack | Medium โญ Repeatedly Asked

Problem Statement: A software security tool validates code syntax. One check is to verify that all brackets are balanced. A string with brackets (, ), {, }, [, ] is balanced if:

  1. Every opening bracket has a corresponding closing bracket.
  2. Brackets are closed in the correct order.
  3. Empty string is considered balanced.

Given a string, determine if brackets are balanced. Print "Balanced" or "Not Balanced".

Constraints:

0 โ‰ค len(S) โ‰ค 10^5
String contains only bracket characters

Input Format:

Single line โ†’ Bracket string

Output Format:

"Balanced" or "Not Balanced"

Sample Input 1:

{[()]}

Sample Output 1:

Balanced

Sample Input 2:

{[(])}

Sample Output 2:

Not Balanced


โ“ Problem 38 โ€” The Wildcard Pattern Matcher

๐Ÿท๏ธ Digital | String/DP | Medium โญ Actual TCS Repeated

Problem Statement: A text search engine allows wildcard pattern matching. The wildcard character ? matches exactly one character, and * matches any sequence of characters (including empty). Given a text string T and a pattern string P, check if P matches T completely.

Print "Match" or "No Match".

Constraints:

1 โ‰ค len(T) โ‰ค 1000
1 โ‰ค len(P) โ‰ค 1000

Input Format:

First line  โ†’ Text string T
Second line โ†’ Pattern string P

Output Format:

"Match" or "No Match"

Sample Input 1:

aab
*b

Sample Output 1:

Match

Sample Input 2:

hello
he?lo

Sample Output 2:

Match

Sample Input 3:

hello
h*rld

Sample Output 3:

No Match


โ“ Problem 39 โ€” The Inventory Merge Sort

๐Ÿท๏ธ Digital | Sorting | Medium

Problem Statement: A large e-commerce company has two sorted inventory lists from two different warehouses, each sorted in ascending order by product ID. The central system needs to merge them into one sorted list efficiently (Merge Sort merge step โ€” O(n+m) time) for unified inventory management.

Constraints:

1 โ‰ค N, M โ‰ค 10^5
1 โ‰ค product_id โ‰ค 10^9

Input Format:

First line  โ†’ Integer N (size of list 1)
Second line โ†’ N space-separated integers (sorted)
Third line  โ†’ Integer M (size of list 2)
Fourth line โ†’ M space-separated integers (sorted)

Output Format:

(N+M) space-separated integers in sorted ascending order

Sample Input 1:

4
1 3 5 7
4
2 4 6 8

Sample Output 1:

1 2 3 4 5 6 7 8


โ“ Problem 40 โ€” The Network Queue Simulation

๐Ÿท๏ธ Digital | Queue/Stack | Medium

Problem Statement: A network router processes data packets using a queue (FIFO). The system receives a sequence of operations:

Simulate the queue operations and produce the output.

Constraints:

1 โ‰ค operations โ‰ค 10^4
1 โ‰ค X โ‰ค 10^6
No DEQUEUE/PEEK on empty queue

Input Format:

First line  โ†’ Integer Q (number of operations)
Next Q lines โ†’ Each operation

Output Format:

Output for DEQUEUE, PEEK, SIZE, ISEMPTY operations (one per line)

Sample Input 1:

6
ENQUEUE 10
ENQUEUE 20
PEEK
DEQUEUE
SIZE
ISEMPTY

Sample Output 1:

10
10
1
Not Empty


โ“ Problem 41 โ€” The Hospital Appointment BFS Scheduler

๐Ÿท๏ธ Digital | Graph/BFS | Medium

Problem Statement: A large hospital has departments connected by corridors. Given a graph of department connections, a patient starts at department 1 and wants to visit all reachable departments using BFS (shortest hop-count order). Print the BFS traversal order starting from node 1.

Constraints:

1 โ‰ค N โ‰ค 100 (departments)
0 โ‰ค E โ‰ค N*(N-1)/2 (corridors, undirected)
Graph may not be fully connected

Input Format:

First line  โ†’ Two integers N E
Next E lines โ†’ Two integers U V (undirected edge)

Output Format:

BFS traversal order starting from node 1, space-separated

Sample Input 1:

6 5
1 2
1 3
2 4
3 5
4 6

Sample Output 1:

1 2 3 4 5 6


โ“ Problem 42 โ€” The IT Department Binary Tree Level Order

๐Ÿท๏ธ Digital | Tree/BFS | Medium

Problem Statement: An IT department stores employee hierarchy as a binary tree, where the root is the CEO. The HR team needs a level-order traversal (BFS) of this tree to generate a company org-chart floor-by-floor.

Given a binary tree via array representation (1-indexed, left child = 2i, right child = 2i+1, -1 means null), print the level-order traversal.

Constraints:

1 โ‰ค N โ‰ค 10^3
-1 represents null node

Input Format:

First line  โ†’ Integer N (number of nodes in array)
Second line โ†’ N space-separated integers (tree array, -1 = null)

Output Format:

Level-order traversal values (skip -1 nodes), space-separated

Sample Input 1:

7
1 2 3 4 5 6 7

Sample Output 1:

1 2 3 4 5 6 7

Sample Input 2:

7
1 2 3 -1 4 5 -1

Sample Output 2:

1 2 3 4 5


โ“ Problem 43 โ€” The Coin Change Minimum

๐Ÿท๏ธ Digital | DP | Medium

Problem Statement: A cashier at a store wants to give back change of amount A using the minimum number of coins. The store has infinite supply of coins of given denominations. If it is not possible to make exact change, print -1.

Constraints:

1 โ‰ค number of denominations โ‰ค 20
1 โ‰ค denomination[i] โ‰ค 1000
1 โ‰ค A โ‰ค 10^4

Input Format:

First line  โ†’ Integer N (number of denominations)
Second line โ†’ N space-separated coin values
Third line  โ†’ Integer A (amount)

Output Format:

Single integer โ†’ Minimum coins needed, or -1 if impossible

Sample Input 1:

3
1 5 10
27

Sample Output 1:

4

Explanation: 27 = 10 + 10 + 5 + 1 + 1 โ†’ Wait, 10+10+5+1+1=27 โ†’ 5 coins. Optimal: 10+10+5+1+1=27 โ†’ min check: 10+10+7 โ†’ 10+10+5+1+1 = 5 coins (candidates compute optimal)



โ“ Problem 44 โ€” The Drone Delivery Graph DFS

๐Ÿท๏ธ Digital | Graph/DFS | Medium

Problem Statement: A drone delivery company maps delivery zones as a directed graph. The central hub (node 1) needs to reach all delivery zones. Using DFS, determine the order drones would explore each zone. Also check if all zones are reachable from node 1; if not, print the unreachable zones.

Constraints:

1 โ‰ค N โ‰ค 100
0 โ‰ค E โ‰ค N*(N-1)
Directed graph

Input Format:

First line  โ†’ Two integers N E
Next E lines โ†’ Two integers U V (directed edge U โ†’ V)

Output Format:

First line  โ†’ "DFS: " followed by DFS order space-separated
Second line โ†’ "Unreachable: " followed by unreachable nodes, or "All Reachable"

Sample Input 1:

5 4
1 2
1 3
2 4
3 5

Sample Output 1:

DFS: 1 2 4 3 5
All Reachable


โ“ Problem 45 โ€” The Longest Common Subsequence Archive

๐Ÿท๏ธ Digital | DP | Mediumโ€“Hard

Problem Statement: Two historians are analyzing ancient texts. They want to find the Longest Common Subsequence (LCS) between two texts (strings) โ€” the longest sequence of characters that appears in both strings in the same order (not necessarily contiguous).

Given two strings, print the length of their LCS.

Constraints:

1 โ‰ค len(S1) โ‰ค 1000
1 โ‰ค len(S2) โ‰ค 1000

Input Format:

First line  โ†’ String S1
Second line โ†’ String S2

Output Format:

Single integer โ†’ Length of LCS

Sample Input 1:

ABCBDAB
BDCABA

Sample Output 1:

4

Explanation: LCS is BCBA or BDAB, length = 4 โœ…



โ“ Problem 46 โ€” The Matrix Diagonal Sum

๐Ÿท๏ธ Digital | Matrix | Medium

Problem Statement: A data analyst is studying a square matrix of Nร—N values. She wants to find the sum of both diagonals (primary and secondary), but if N is odd, the center element should be counted only once (not twice).

Constraints:

1 โ‰ค N โ‰ค 100
1 โ‰ค matrix[i][j] โ‰ค 10^6

Input Format:

First line  โ†’ Integer N
Next N lines โ†’ N space-separated integers per row

Output Format:

Single integer โ†’ Sum of both diagonals (center counted once)

Sample Input 1:

3
1 2 3
4 5 6
7 8 9

Sample Output 1:

25

Explanation: Primary diagonal: 1+5+9=15. Secondary: 3+5+7=15. Center (5) counted twice โ†’ 15+15-5=25 โœ…



โ“ Problem 47 โ€” The Jump Game Solution

๐Ÿท๏ธ Digital | Greedy/DP | Medium

Problem Statement: A video game character is positioned at index 0 of an array. Each element represents the maximum jump length from that position. The character wants to reach the last index. Determine if it is possible to reach the last index.

Print "Can Reach" or "Cannot Reach".

Constraints:

1 โ‰ค N โ‰ค 10^4
0 โ‰ค arr[i] โ‰ค 10^4

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated integers

Output Format:

"Can Reach" or "Cannot Reach"

Sample Input 1:

6
2 3 1 1 4 0

Sample Output 1:

Can Reach

Sample Input 2:

6
3 2 1 0 4 0

Sample Output 2:

Cannot Reach


โ“ Problem 48 โ€” The Student Distribution Problem (Combinatorics)

๐Ÿท๏ธ Digital | Math/Combinatorics | Hard

Problem Statement: A teacher wants to distribute N students equally into R classrooms. The number of ways to arrange students in classrooms (where arrangement within classroom matters) is calculated using a multinomial coefficient. Compute the number of ways modulo 10^9+7.

Given R rooms and N students, find the count of arrangements where students are distributed as equally as possible (some rooms get floor(N/R) students, others get ceil(N/R)).

Constraints:

1 โ‰ค R โ‰ค N โ‰ค 10^6

Input Format:

First line  โ†’ Integer T (test cases)
Next T lines โ†’ Two integers R N

Output Format:

T lines โ†’ Answer for each test case modulo 10^9+7

Sample Input 1:

2
3 6
2 5

Sample Output 1:

90
20


โ“ Problem 49 โ€” The Smart City Traffic Signal

๐Ÿท๏ธ Digital | Graph/Shortest Path | Mediumโ€“Hard

Problem Statement: A smart city has N intersections and M roads. Each road has a travel time (weight). An ambulance at intersection S needs the shortest time to reach the hospital at intersection D (Dijkstra’s Algorithm).

Constraints:

1 โ‰ค N โ‰ค 1000
1 โ‰ค M โ‰ค 10^4
1 โ‰ค weight โ‰ค 10^4
1 โ‰ค S, D โ‰ค N

Input Format:

First line  โ†’ Two integers N M
Next M lines โ†’ Three integers U V W (edge from U to V with weight W, undirected)
Last line   โ†’ Two integers S D

Output Format:

Single integer โ†’ Shortest time from S to D, or -1 if unreachable

Sample Input 1:

4 4
1 2 10
1 3 5
3 2 3
2 4 1
1 4

Sample Output 1:

9

Explanation: Path 1โ†’3โ†’2โ†’4: 5+3+1 = 9 (shorter than 1โ†’2โ†’4: 10+1=11) โœ…



โ“ Problem 50 โ€” The Anagram Grouper

๐Ÿท๏ธ Digital | String/HashMap | Medium

Problem Statement: A linguistics researcher has a list of N words. She wants to group all anagrams together and print each group on a separate line (words within a group separated by spaces, groups in order of first occurrence).

Constraints:

1 โ‰ค N โ‰ค 10^4
1 โ‰ค len(word) โ‰ค 100
Words are lowercase

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated words

Output Format:

Each group of anagrams on a separate line

Sample Input 1:

6
eat tea tan ate nat bat

Sample Output 1:

eat tea ate
tan nat
bat


โ“ Problem 51 โ€” The Rectangle Overlap Detector

๐Ÿท๏ธ Digital | Geometry | Medium

Problem Statement: A GIS software system detects if two land plots (rectangles) overlap. Each rectangle is defined by its bottom-left (x1, y1) and top-right (x2, y2) coordinates. Two rectangles overlap if their intersection has positive area (touching at boundary only is NOT overlap).

Print "Overlap" or "No Overlap".

Constraints:

-10^4 โ‰ค coordinates โ‰ค 10^4

Input Format:

First line  โ†’ x1 y1 x2 y2 (Rectangle 1)
Second line โ†’ x3 y3 x4 y4 (Rectangle 2)

Output Format:

"Overlap" or "No Overlap"

Sample Input 1:

0 0 4 4
2 2 6 6

Sample Output 1:

Overlap

Sample Input 2:

0 0 2 2
3 3 5 5

Sample Output 2:

No Overlap


โ“ Problem 52 โ€” The Word Ladder Distance

๐Ÿท๏ธ Digital | BFS/Graph | Hard

Problem Statement: In a word transformation game, a player must transform word BEGIN to word TARGET by changing exactly one letter at a time, where each intermediate word must exist in a given dictionary. Find the minimum number of steps (transformations) needed. If impossible, print 0.

Constraints:

2 โ‰ค word length โ‰ค 5 (all words same length)
1 โ‰ค dictionary size โ‰ค 500

Input Format:

First line  โ†’ String BEGIN
Second line โ†’ String TARGET
Third line  โ†’ Integer N (dictionary size)
Next N lines โ†’ Dictionary words (one per line)

Output Format:

Single integer โ†’ Minimum transformations (or 0 if impossible)

Sample Input 1:

hit
cog
6
hot
dot
dog
lot
log
cog

Sample Output 1:

5

Explanation: hit โ†’ hot โ†’ dot โ†’ dog โ†’ cog (5 steps) โœ…



โ“ Problem 53 โ€” The Largest Rectangle in Histogram

๐Ÿท๏ธ Digital | Stack | Hard

Problem Statement: An architect is analyzing a city skyline represented as a histogram (bar chart) where each bar has width 1 and a given height. She wants to find the area of the largest rectangle that can be formed within the histogram bars.

Constraints:

1 โ‰ค N โ‰ค 10^5
0 โ‰ค height[i] โ‰ค 10^4

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated integers (bar heights)

Output Format:

Single integer โ†’ Maximum rectangle area

Sample Input 1:

6
2 1 5 6 2 3

Sample Output 1:

10

Explanation: Bars of height 5 and 6 form a rectangle of area 10 โœ…



โ“ Problem 54 โ€” The Network Infection Spread (BFS Levels)

๐Ÿท๏ธ Digital | BFS/Graph | Medium

Problem Statement: A cybersecurity analyst is simulating a virus spread through N computers (nodes 1 to N) connected by a network graph. The virus starts at node 1 at time 0. Every second, it infects all directly connected uninfected computers simultaneously. Find the time taken to infect all computers, and if any computer is unreachable, print its list.

Constraints:

1 โ‰ค N โ‰ค 1000
Undirected graph

Input Format:

First line  โ†’ Two integers N E
Next E lines โ†’ Two integers U V (undirected edge)

Output Format:

First line  โ†’ "Infection Time: T seconds"
Second line โ†’ "Unreachable: X Y Z" or "All Infected"

Sample Input 1:

5 4
1 2
1 3
2 4
3 5

Sample Output 1:

Infection Time: 2 seconds
All Infected


โ“ Problem 55 โ€” The Maximum Path Sum in Binary Tree

๐Ÿท๏ธ Digital | Tree/DFS | Hard

Problem Statement: A financial planning company represents investment plans as a binary tree where each node holds a profit/loss value (can be negative). An analyst wants to find the maximum path sum โ€” the maximum sum achievable along any path from any node to any node (path must go through connected nodes, can start/end anywhere).

Constraints:

1 โ‰ค N โ‰ค 10^4
-10^4 โ‰ค node_val โ‰ค 10^4

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated integers (tree in level-order, -1 = null)

Output Format:

Single integer โ†’ Maximum path sum

Sample Input 1:

5
-10 9 20 -1 -1 15 7

Sample Output 1:

42

Explanation: Path: 15 โ†’ 20 โ†’ 7 = 42 โœ…



โ“ Problem 56 โ€” The Minimum Spanning Tree Cost

๐Ÿท๏ธ Digital | Graph/MST | Hard

Problem Statement: A telecom company wants to lay fibre cables connecting N cities with minimum total cable length. Given the cost of laying cable between each pair of cities, find the Minimum Spanning Tree cost using Kruskal’s or Prim’s algorithm.

Constraints:

2 โ‰ค N โ‰ค 500
Weighted undirected connected graph

Input Format:

First line  โ†’ Two integers N E
Next E lines โ†’ Three integers U V W (edge with weight)

Output Format:

Single integer โ†’ Total MST cost

Sample Input 1:

4 5
1 2 10
1 3 6
1 4 5
2 4 15
3 4 4

Sample Output 1:

19

Explanation: MST edges: 3-4(4) + 1-4(5) + 1-2(10) = 19 โœ…



โ“ Problem 57 โ€” The Palindrome Partitioning Count

๐Ÿท๏ธ Digital | DP | Hard

Problem Statement: A data encoding system partitions a string into substrings where every substring is a palindrome. The system wants to know the minimum number of cuts needed to partition a given string S such that every resulting substring is a palindrome.

Constraints:

1 โ‰ค len(S) โ‰ค 1000
String contains lowercase letters

Input Format:

Single line โ†’ String S

Output Format:

Single integer โ†’ Minimum number of cuts

Sample Input 1:

aab

Sample Output 1:

1

Explanation: aab โ†’ aa | b (1 cut) โœ…



โ“ Problem 58 โ€” The Topological Sort Curriculum

๐Ÿท๏ธ Digital | Graph/DAG | Mediumโ€“Hard

Problem Statement: A university curriculum has N courses. Some courses have prerequisites. Given a list of prerequisite pairs (A, B meaning A must be taken before B), produce a valid course order using Topological Sort. If there’s a cycle (impossible to complete all courses), print "Impossible".

Constraints:

1 โ‰ค N โ‰ค 100
Directed acyclic graph (or cyclic โ€” candidate must detect)

Input Format:

First line  โ†’ Two integers N E
Next E lines โ†’ Two integers A B (A is prerequisite of B)

Output Format:

Space-separated topological order, or "Impossible"

Sample Input 1:

4 3
1 2
1 3
3 4

Sample Output 1:

1 3 4 2

(Any valid topological order is accepted)



โ“ Problem 59 โ€” The Flood Fill Algorithm

๐Ÿท๏ธ Digital | Graph/DFS | Medium

Problem Statement: A paint application has a canvas represented as an Nร—M grid of integers (colors). A user clicks on cell (SR, SC) and selects a new color NC. The flood fill operation changes the color of the clicked cell and all adjacent (4-directional) cells with the same original color to the new color โ€” recursively.

Print the grid after flood fill.

Constraints:

1 โ‰ค N, M โ‰ค 50
0 โ‰ค color โ‰ค 100

Input Format:

First line  โ†’ Two integers N M
Next N lines โ†’ M space-separated integers (grid)
Next line   โ†’ Three integers SR SC NC

Output Format:

N lines of M space-separated integers (modified grid)

Sample Input 1:

3 3
1 1 1
1 1 0
1 0 0
1 1 2

Sample Output 1:

2 2 2
2 2 0
2 0 0


โ“ Problem 60 โ€” The Sliding Window Maximum

๐Ÿท๏ธ Digital | Deque | Hard

Problem Statement: A financial analyst tracks stock prices over N days. She uses a sliding window of size K to find the maximum price in each window as it moves from left to right across the data. Efficient O(n) solution is expected (use deque).

Constraints:

1 โ‰ค K โ‰ค N โ‰ค 10^5
1 โ‰ค price[i] โ‰ค 10^6

Input Format:

First line  โ†’ Two integers N K
Second line โ†’ N space-separated integers (prices)

Output Format:

(N-K+1) space-separated integers โ†’ Max of each window

Sample Input 1:

8 3
1 3 -1 -3 5 3 6 7

Sample Output 1:

3 3 5 5 6 7


โ“ Problem 61 โ€” The Matrix Chain Multiplication Order

๐Ÿท๏ธ Digital | DP | Hard

Problem Statement: A computer graphics system needs to multiply a chain of N matrices. The order of multiplication affects the total number of scalar multiplications needed. Given the dimensions of N matrices (as an array where matrix i has dimensions arr[i-1] ร— arr[i]), find the minimum number of scalar multiplications needed.

Constraints:

2 โ‰ค N โ‰ค 100
1 โ‰ค arr[i] โ‰ค 100

Input Format:

First line  โ†’ Integer N (number of matrices)
Second line โ†’ (N+1) space-separated integers (dimension array)

Output Format:

Single integer โ†’ Minimum scalar multiplications

Sample Input 1:

3
40 20 30 10

Sample Output 1:

26000


โ“ Problem 62 โ€” The Rat in a Maze Pathfinder

๐Ÿท๏ธ Digital | Backtracking | Mediumโ€“Hard

Problem Statement: A lab rat starts at the top-left corner of an Nร—N maze (cell 0,0) and needs to reach the bottom-right corner (N-1, N-1). The maze is represented as a 2D grid where 1 = open path and 0 = wall. The rat can move Down (D) or Right (R) only. Print all valid paths in lexicographic order. If no path exists, print "No Path".

Constraints:

2 โ‰ค N โ‰ค 10
maze[i][j] โˆˆ {0, 1}

Input Format:

First line  โ†’ Integer N
Next N lines โ†’ N space-separated integers (maze row)

Output Format:

All valid paths as strings of D/R characters, one per line

Sample Input 1:

4
1 0 0 0
1 1 0 1
1 1 0 0
0 1 1 1

Sample Output 1:

DDRDRR
DDRRD

(exact output depends on direction rules โ€” candidates compute)



โ“ Problem 63 โ€” The N-Queen Placement

๐Ÿท๏ธ Digital | Backtracking | Hard

Problem Statement: The classic N-Queens problem: Place N queens on an Nร—N chessboard such that no two queens attack each other (no two in same row, column, or diagonal). Print the total number of distinct solutions.

Constraints:

1 โ‰ค N โ‰ค 12

Input Format:

Single line โ†’ Integer N

Output Format:

Single integer โ†’ Number of distinct solutions

Sample Input 1:

4

Sample Output 1:

2

Sample Input 2:

8

Sample Output 2:

92


โ“ Problem 64 โ€” The Trie-Based Autocomplete

๐Ÿท๏ธ Digital | Trie | Hard

Problem Statement: A search engine builds an autocomplete system using a Trie data structure. Given a dictionary of N words, and Q prefix queries, for each query print all words in the dictionary that start with that prefix. If no words match, print "No Suggestions". Words should be printed in lexicographic order.

Constraints:

1 โ‰ค N โ‰ค 1000
1 โ‰ค Q โ‰ค 100
All strings contain lowercase letters

Input Format:

First line  โ†’ Integer N
Next N lines โ†’ Dictionary words
Next line   โ†’ Integer Q
Next Q lines โ†’ Prefix queries

Output Format:

For each query: matching words comma-separated, or "No Suggestions"

Sample Input 1:

5
apple
app
application
apt
bat
2
app
ba

Sample Output 1:

app,apple,application
bat


โ“ Problem 65 โ€” The Subarray Product Less Than K

๐Ÿท๏ธ Digital | Sliding Window | Medium

Problem Statement: A factory’s production line records daily output. A quality supervisor wants to know how many contiguous subarrays have a product strictly less than K. Each element is a positive integer.

Constraints:

1 โ‰ค N โ‰ค 3 ร— 10^4
1 โ‰ค arr[i] โ‰ค 1000
0 < K โ‰ค 10^6

Input Format:

First line  โ†’ Two integers N K
Second line โ†’ N space-separated positive integers

Output Format:

Single integer โ†’ Count of subarrays with product < K

Sample Input 1:

4 100
10 5 2 6

Sample Output 1:

8


โ“ Problem 66 โ€” The Serialize and Deserialize Binary Tree

๐Ÿท๏ธ Digital | Tree | Hard

Problem Statement: A cloud backup system serializes binary trees to strings for storage and deserializes them back. Serialize a binary tree to a comma-separated string (use "null" for missing nodes, level-order). Then deserialize back and print in-order traversal to verify correctness.

Constraints:

0 โ‰ค N โ‰ค 100 nodes
-1000 โ‰ค node_val โ‰ค 1000

Input Format:

First line  โ†’ Comma-separated serialized tree (level-order, "null" for empty)

Output Format:

First line  โ†’ Serialized string (as-is)
Second line โ†’ In-order traversal of deserialized tree

Sample Input 1:

1,2,3,null,null,4,5

Sample Output 1:

1,2,3,null,null,4,5
2 1 4 3 5


โ“ Problem 67 โ€” The Prison Break Grid

๐Ÿท๏ธ Digital | BFS/Grid | Medium

Problem Statement: A prisoner is trapped in an Nร—M grid cell (SR, SC) and needs to reach the exit at (ER, EC). The grid has walls (#) and open cells (.). The prisoner can move in 4 directions (Up, Down, Left, Right). Find the minimum steps to escape, or print -1 if impossible.

Constraints:

1 โ‰ค N, M โ‰ค 200

Input Format:

First line  โ†’ Two integers N M
Next N lines โ†’ Grid rows (each character is . or #)
Next line   โ†’ SR SC ER EC (start and end positions, 0-indexed)

Output Format:

Single integer โ†’ Minimum steps, or -1

Sample Input 1:

5 5
. . . . .
. # # # .
. . . # .
# # . # .
. . . . .
0 0 4 4

Sample Output 1:

8


โ“ Problem 68 โ€” The Alien Dictionary Order

๐Ÿท๏ธ Digital | Topological Sort | Hard

Problem Statement: Scientists discover an alien civilization’s dictionary. The dictionary is sorted in the alien language’s own alphabetical order. Given a sorted list of alien words, deduce the order of letters in the alien alphabet (topological sort on characters). If the order is ambiguous, output any valid order. If it is impossible (cycle), print "Invalid Dictionary".

Constraints:

2 โ‰ค N โ‰ค 100 words
All characters are lowercase English letters

Input Format:

First line  โ†’ Integer N
Next N lines โ†’ Alien words in sorted order

Output Format:

String โ†’ Deduced letter ordering, or "Invalid Dictionary"

Sample Input 1:

4
baa
abcd
abca
cab

Sample Output 1:

bdac

(Any valid topological order)



โ“ Problem 69 โ€” The Sudoku Validator

๐Ÿท๏ธ Digital | Matrix/Logic | Medium

Problem Statement: A puzzle verification system checks submitted 9ร—9 Sudoku boards. A filled Sudoku board is valid if:

  1. Each row contains digits 1โ€“9 with no repetition
  2. Each column contains digits 1โ€“9 with no repetition
  3. Each of the 9 3ร—3 sub-boxes contains digits 1โ€“9 with no repetition

Print "Valid Sudoku" or "Invalid Sudoku".

Constraints:

Exactly 9 rows and 9 columns
All values are integers 1โ€“9

Input Format:

9 lines โ†’ 9 space-separated integers per line

Output Format:

"Valid Sudoku" or "Invalid Sudoku"

Sample Input 1:

5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9

Sample Output 1:

Valid Sudoku


โ“ Problem 70 โ€” The Knapsack Decision

๐Ÿท๏ธ Digital | DP/Greedy | Mediumโ€“Hard

Problem Statement: A thief is robbing a jewellery shop and can carry a bag of maximum weight capacity W. There are N items, each with a weight and a value. The thief wants to maximize the total value without exceeding W. Items cannot be split (0/1 Knapsack).

Constraints:

1 โ‰ค N โ‰ค 100
1 โ‰ค W โ‰ค 1000
1 โ‰ค weight[i] โ‰ค W
1 โ‰ค value[i] โ‰ค 10^4

Input Format:

First line  โ†’ Two integers N W
Next N lines โ†’ Two integers weight value

Output Format:

Single integer โ†’ Maximum value achievable

Sample Input 1:

4 7
1 1
3 4
4 5
5 7

Sample Output 1:

9



๐Ÿ”ด TCS PRIME โ€” EXPERT LEVEL (Q71โ€“Q100)


โ“ Problem 71 โ€” The Alien Invasion Segment Tree

๐Ÿท๏ธ Prime | Segment Tree | Very Hard

Problem Statement: An alien tracking system monitors N radar readings. It supports two operations repeatedly in real-time:

  1. UPDATE i x โ†’ Update the reading at position i to x
  2. QUERY l r โ†’ Find the maximum reading in range [l, r]

Use a Segment Tree to handle both operations in O(log n).

Constraints:

1 โ‰ค N โ‰ค 10^5
1 โ‰ค Q โ‰ค 10^5
1 โ‰ค i, l, r โ‰ค N
1 โ‰ค x โ‰ค 10^9

Input Format:

First line  โ†’ Two integers N Q
Second line โ†’ N space-separated integers (initial readings)
Next Q lines โ†’ "UPDATE i x" or "QUERY l r"

Output Format:

For each QUERY, print the maximum value on a new line

Sample Input 1:

5 4
1 3 5 7 9
QUERY 1 3
UPDATE 2 10
QUERY 1 3
QUERY 3 5

Sample Output 1:

5
10
9


โ“ Problem 72 โ€” The Magical Fenwick Tree Range Sum

๐Ÿท๏ธ Prime | BIT/Fenwick Tree | Very Hard

Problem Statement: A financial system tracks N accounts with initial balances. It processes:

  1. ADD i x โ†’ Add x to account i
  2. SUM l r โ†’ Print total balance from account l to r

Use a Binary Indexed Tree (Fenwick Tree) for O(log n) per operation.

Constraints:

1 โ‰ค N โ‰ค 10^6
1 โ‰ค Q โ‰ค 10^6

Input Format:

First line  โ†’ Two integers N Q
Second line โ†’ N initial balances
Next Q lines โ†’ "ADD i x" or "SUM l r"

Output Format:

Answer for each SUM query on a new line

Sample Input 1:

5 3
1 2 3 4 5
SUM 1 3
ADD 2 5
SUM 1 3

Sample Output 1:

6
11


โ“ Problem 73 โ€” The Interstellar LRU Cache

๐Ÿท๏ธ Prime | HashMap + Doubly Linked List | Very Hard

Problem Statement: A space station computer uses an LRU (Least Recently Used) cache of capacity C. It processes GET and PUT operations:

Implement the LRU cache and simulate all operations.

Constraints:

1 โ‰ค C โ‰ค 3000
1 โ‰ค operations โ‰ค 2 ร— 10^4

Input Format:

First line  โ†’ Two integers C Q
Next Q lines โ†’ "GET key" or "PUT key value"

Output Format:

For each GET, print the value or -1

Sample Input 1:

2 7
PUT 1 1
PUT 2 2
GET 1
PUT 3 3
GET 2
PUT 4 4
GET 1

Sample Output 1:

1
-1
1


โ“ Problem 74 โ€” The Maximum Flow Water Distribution

๐Ÿท๏ธ Prime | Graph/Max Flow | Very Hard

Problem Statement: A city engineer is designing a water distribution network modelled as a directed graph with N nodes. Each edge has a maximum flow capacity. Find the maximum water flow from source (node 1) to sink (node N) using the Ford-Fulkerson algorithm.

Constraints:

2 โ‰ค N โ‰ค 50
Directed graph with capacities

Input Format:

First line  โ†’ Two integers N E
Next E lines โ†’ Three integers U V C (directed edge from U to V with capacity C)

Output Format:

Single integer โ†’ Maximum flow from 1 to N

Sample Input 1:

6 9
1 2 16
1 3 13
2 3 10
2 4 12
3 2 4
3 5 14
4 3 9
4 6 20
5 6 4

Sample Output 1:

23


โ“ Problem 75 โ€” The String Hashing Anti-Plagiarism

๐Ÿท๏ธ Prime | Hashing/Rolling Hash | Hard

Problem Statement: A university anti-plagiarism system detects if any substring of document A of length L exactly matches any substring of document B of length L. Use Rabin-Karp rolling hash for efficiency. Print the starting indices (0-based) in both documents where the first match is found, or "No Match".

Constraints:

1 โ‰ค len(A), len(B) โ‰ค 10^5
1 โ‰ค L โ‰ค min(len(A), len(B))

Input Format:

First line  โ†’ String A
Second line โ†’ String B
Third line  โ†’ Integer L

Output Format:

"Match at A[i] and B[j]" or "No Match"

Sample Input 1:

abcxyz
xyzabc
3

Sample Output 1:

Match at A[0] and B[3]


โ“ Problem 76 โ€” The Minimum Cost to Hire Workers

๐Ÿท๏ธ Prime | Greedy/Heap | Hard

Problem Statement: A company wants to hire exactly K workers from a pool of N workers. Each worker has a quality and a wage expectation. The company must pay each worker at least their expected wage AND proportional to quality (all workers in the group are paid at the same wage-to-quality ratio). Find the minimum cost to hire exactly K workers (output as a float rounded to 5 decimal places).

Constraints:

1 โ‰ค K โ‰ค N โ‰ค 10^4
1 โ‰ค quality[i] โ‰ค 10^4
1 โ‰ค wage[i] โ‰ค 10^4

Input Format:

First line  โ†’ Two integers N K
Next N lines โ†’ Two integers quality wage

Output Format:

Minimum cost rounded to 5 decimal places

Sample Input 1:

3 2
10 70
20 50
5 30

Sample Output 1:

105.00000


โ“ Problem 77 โ€” The Burrows-Wheeler String Transform

๐Ÿท๏ธ Prime | String/Cyclic Rotation | Very Hard

Problem Statement: The Burrows-Wheeler Transform (BWT) is used in data compression. Given a string S, generate all rotations, sort them lexicographically, and output the last column of the sorted rotation matrix. Also output the row index where the original string appears in the sorted order (0-indexed).

Constraints:

1 โ‰ค len(S) โ‰ค 1000
String contains lowercase letters only

Input Format:

Single line โ†’ String S

Output Format:

First line  โ†’ BWT string (last column)
Second line โ†’ Index of original string

Sample Input 1:

banana

Sample Output 1:

annb$aa

(Standard BWT appends $ to mark end โ€” candidates follow convention)



โ“ Problem 78 โ€” The Minimum Edit Distance Engine

๐Ÿท๏ธ Prime | DP | Hard

Problem Statement: A spell-checker computes how many minimum operations (insert, delete, or replace one character) are needed to transform word A into word B. This is the Edit Distance (Levenshtein Distance). Given two words, compute it.

Constraints:

0 โ‰ค len(A), len(B) โ‰ค 1000

Input Format:

First line  โ†’ String A
Second line โ†’ String B

Output Format:

Single integer โ†’ Minimum edit distance

Sample Input 1:

horse
ros

Sample Output 1:

3

Sample Input 2:

intention
execution

Sample Output 2:

5


โ“ Problem 79 โ€” The Tarjan’s SCC Finder

๐Ÿท๏ธ Prime | Graph/DFS/SCC | Very Hard

Problem Statement: A telecommunications company has N servers connected by directed links. They want to identify Strongly Connected Components (SCCs) โ€” groups of servers where every server can reach every other server in the group. Use Tarjan’s Algorithm and print each SCC.

Constraints:

1 โ‰ค N โ‰ค 1000
Directed graph

Input Format:

First line  โ†’ Two integers N E
Next E lines โ†’ Two integers U V (directed edge)

Output Format:

Print each SCC on a separate line (nodes space-separated, in any order)

Sample Input 1:

5 5
1 2
2 3
3 1
3 4
4 5

Sample Output 1:

1 2 3
4
5


โ“ Problem 80 โ€” The Convex Hull Security Perimeter

๐Ÿท๏ธ Prime | Geometry/Convex Hull | Very Hard

Problem Statement: A military base needs to erect a fence around all its watchtowers. Given N watchtower coordinates, find the minimum perimeter fence that encloses all towers โ€” i.e., the Convex Hull of the points. Use Graham Scan or Jarvis March. Print the vertices of the convex hull in counter-clockwise order starting from the bottom-most point, and the perimeter length rounded to 2 decimal places.

Constraints:

3 โ‰ค N โ‰ค 10^4
0 โ‰ค x, y โ‰ค 10^4

Input Format:

First line  โ†’ Integer N
Next N lines โ†’ Two integers x y (coordinates)

Output Format:

First line  โ†’ Convex hull vertices (x,y pairs) in CCW order
Second line โ†’ "Perimeter: X.XX"

Sample Input 1:

5
0 0
1 1
2 2
0 2
2 0

Sample Output 1:

(0,0) (2,0) (2,2) (0,2)
Perimeter: 8.00


โ“ Problem 81 โ€” The Alien Number Theory (Big Modular Exponentiation)

๐Ÿท๏ธ Prime | Math/Number Theory | Hard

Problem Statement: A cryptography algorithm requires computing (A^B) mod M where A and B can be extremely large. Brute force is too slow โ€” use fast modular exponentiation (binary exponentiation) to compute it efficiently in O(log B) time.

Constraints:

1 โ‰ค A โ‰ค 10^18
1 โ‰ค B โ‰ค 10^18
1 โ‰ค M โ‰ค 10^9

Input Format:

Three integers on separate lines: A, B, M

Output Format:

Single integer โ†’ (A^B) mod M

Sample Input 1:

2
10
1000

Sample Output 1:

24

Sample Input 2:

3
200
1000000007

Sample Output 2:

884337246


โ“ Problem 82 โ€” The Longest Increasing Subsequence with Count

๐Ÿท๏ธ Prime | DP | Hard

Problem Statement: A stock analyst is analyzing stock prices over N days. She wants to find both the length of the Longest Increasing Subsequence (LIS) and the number of such subsequences of that length. Print both values. Count modulo 10^9+7.

Constraints:

1 โ‰ค N โ‰ค 2000
1 โ‰ค price[i] โ‰ค 10^9

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated integers

Output Format:

First line  โ†’ "LIS Length: X"
Second line โ†’ "Count: Y"

Sample Input 1:

6
1 3 5 4 7 8

Sample Output 1:

LIS Length: 5
Count: 2


โ“ Problem 83 โ€” The Painter’s Partition Problem

๐Ÿท๏ธ Prime | Binary Search + Greedy | Hard

Problem Statement: N boards need to be painted. There are K painters. Each painter can paint contiguous boards only. The time taken to paint a board = board length. All painters work simultaneously. Minimize the time taken by the slowest painter (i.e., minimize the maximum sum of any contiguous partition of N boards into K groups).

Constraints:

1 โ‰ค K โ‰ค N โ‰ค 10^4
1 โ‰ค board[i] โ‰ค 10^6

Input Format:

First line  โ†’ Two integers N K
Second line โ†’ N space-separated integers (board lengths)

Output Format:

Single integer โ†’ Minimum time (maximum partition sum minimized)

Sample Input 1:

4 2
10 20 30 40

Sample Output 1:

60

Explanation: Optimal partition: [10,20,30] and [40] โ†’ max(60,40) = 60 โœ…



โ“ Problem 84 โ€” The Interval Scheduling Meeting Rooms

๐Ÿท๏ธ Prime | Greedy/Sorting | Hard

Problem Statement: A conference centre manager receives N meeting requests. Each meeting has a start and end time. She wants to find the minimum number of meeting rooms required to accommodate all meetings (no two meetings in the same room can overlap).

Constraints:

1 โ‰ค N โ‰ค 10^5
0 โ‰ค start[i] < end[i] โ‰ค 10^9

Input Format:

First line  โ†’ Integer N
Next N lines โ†’ Two integers start end

Output Format:

Single integer โ†’ Minimum rooms required

Sample Input 1:

4
0 30
5 10
15 20
5 25

Sample Output 1:

3


โ“ Problem 85 โ€” The Regular Expression Matcher

๐Ÿท๏ธ Prime | DP/String | Very Hard

Problem Statement: Implement a regular expression matcher supporting:

The match must cover the entire input string (not partial). Print "Match" or "No Match".

Constraints:

0 โ‰ค len(s), len(p) โ‰ค 1000
s contains lowercase letters
p contains lowercase letters, '.', '*'

Input Format:

First line  โ†’ String s (text)
Second line โ†’ String p (pattern)

Output Format:

"Match" or "No Match"

Sample Input 1:

aa
a*

Sample Output 1:

Match

Sample Input 2:

ab
.*

Sample Output 2:

Match

Sample Input 3:

aab
c*a*b

Sample Output 3:

Match


โ“ Problem 86 โ€” The Minimum Window Substring

๐Ÿท๏ธ Prime | Sliding Window | Hard

Problem Statement: A search engine’s highlight feature needs to find the minimum window substring of string S that contains all characters of string T (including duplicates). If no valid window exists, print "".

Constraints:

1 โ‰ค len(S) โ‰ค 10^5
1 โ‰ค len(T) โ‰ค 10^4

Input Format:

First line  โ†’ String S
Second line โ†’ String T

Output Format:

Minimum window substring, or "" if not possible

Sample Input 1:

ADOBECODEBANC
ABC

Sample Output 1:

BANC

Sample Input 2:

a
aa

Sample Output 2:




โ“ Problem 87 โ€” The Number of Islands (Union-Find)

๐Ÿท๏ธ Prime | Union-Find/DFS | Hard

Problem Statement: A geographer is analysing satellite images. A 2D grid contains 1 (land) and 0 (water). An island is a group of 1s connected horizontally or vertically. Count the total number of distinct islands. Use Union-Find (Disjoint Set Union) for maximum efficiency.

Constraints:

1 โ‰ค N, M โ‰ค 300
grid[i][j] โˆˆ {0, 1}

Input Format:

First line  โ†’ Two integers N M
Next N lines โ†’ M space-separated integers (0 or 1)

Output Format:

Single integer โ†’ Number of islands

Sample Input 1:

4 5
1 1 1 1 0
1 1 0 1 0
1 1 0 0 0
0 0 0 0 0

Sample Output 1:

1

Sample Input 2:

4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1

Sample Output 2:

3


โ“ Problem 88 โ€” The Traveling Salesman Problem (Small N)

๐Ÿท๏ธ Prime | DP/Bitmask | Very Hard

Problem Statement: A delivery agent starts from city 0 and must visit all N cities exactly once, then return to city 0. Given the distance matrix, find the minimum total travel distance (TSP using Bitmask DP for N โ‰ค 20).

Constraints:

2 โ‰ค N โ‰ค 15
0 โ‰ค distance[i][j] โ‰ค 10^4
distance[i][i] = 0

Input Format:

First line  โ†’ Integer N
Next N lines โ†’ N space-separated integers (distance matrix row i)

Output Format:

Single integer โ†’ Minimum tour distance

Sample Input 1:

4
0 10 15 20
10 0 35 25
15 35 0 30
20 25 30 0

Sample Output 1:

80


โ“ Problem 89 โ€” The Suffix Array Construction

๐Ÿท๏ธ Prime | String/Suffix Array | Very Hard

Problem Statement: A bioinformatics tool indexes DNA sequences using a Suffix Array โ€” an array of the starting indices of all suffixes of a string, sorted in lexicographic order. Given a DNA string S, construct and output its suffix array.

Constraints:

1 โ‰ค len(S) โ‰ค 10^5
String contains only A, C, G, T

Input Format:

Single line โ†’ DNA string S

Output Format:

Space-separated integers โ†’ Suffix array (starting indices 0-indexed)

Sample Input 1:

ACGT

Sample Output 1:

0 1 2 3

Sample Input 2:

banana

Sample Output 2:

5 3 1 0 4 2


โ“ Problem 90 โ€” The Count of Inversions (Merge Sort)

๐Ÿท๏ธ Prime | Divide & Conquer | Hard

Problem Statement: A stock market analyst counts inversions in a price array โ€” a pair (i, j) where i < j but arr[i] > arr[j] represents a bad trend. Count the total number of inversions using modified Merge Sort in O(n log n).

Constraints:

1 โ‰ค N โ‰ค 10^5
1 โ‰ค arr[i] โ‰ค 10^9

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated integers

Output Format:

Single integer โ†’ Number of inversions

Sample Input 1:

5
2 4 1 3 5

Sample Output 1:

3

Explanation: Inversions: (2,1), (4,1), (4,3) โ†’ 3 โœ…



โ“ Problem 91 โ€” The Skip List Implementation

๐Ÿท๏ธ Prime | Advanced Data Structure | Very Hard

Problem Statement: A database system uses a Skip List for fast search, insert, and delete. Implement a Skip List and simulate Q operations:

Constraints:

1 โ‰ค Q โ‰ค 10^4
1 โ‰ค x โ‰ค 10^6

Input Format:

First line  โ†’ Integer Q
Next Q lines โ†’ Operation and value

Output Format:

Output for SEARCH and PRINT operations

Sample Input 1:

6
INSERT 5
INSERT 3
INSERT 7
SEARCH 3
DELETE 3
PRINT

Sample Output 1:

Found
5 7


โ“ Problem 92 โ€” The Game Theory Nim Problem

๐Ÿท๏ธ Prime | Game Theory | Hard

Problem Statement: Two players play Nim: there are N piles of stones. Players take turns. On each turn a player removes any positive number of stones from exactly one pile. The player who takes the last stone wins. Both players play optimally.

Given the pile sizes, determine who wins: “First Player Wins” or “Second Player Wins”.

The optimal strategy uses XOR: if XOR of all pile sizes โ‰  0, first player wins.

Constraints:

1 โ‰ค N โ‰ค 100
0 โ‰ค pile[i] โ‰ค 10^9

Input Format:

First line  โ†’ Integer N
Second line โ†’ N space-separated pile sizes

Output Format:

"First Player Wins" or "Second Player Wins"

Sample Input 1:

3
3 4 5

Sample Output 1:

First Player Wins

Sample Input 2:

3
1 2 3

Sample Output 2:

Second Player Wins

Explanation: XOR(3,4,5) = 2 โ‰  0 โ†’ First wins. XOR(1,2,3) = 0 โ†’ Second wins.



โ“ Problem 93 โ€” The Shortest Superstring

๐Ÿท๏ธ Prime | DP/Greedy | Very Hard

Problem Statement: A DNA sequencing lab receives N DNA fragments. They want to reconstruct the shortest possible DNA strand that contains all fragments as substrings. This is the Shortest Superstring Problem. For small N (โ‰ค 12), use Bitmask DP. Return the minimum length of such a superstring.

Constraints:

1 โ‰ค N โ‰ค 12
1 โ‰ค len(each fragment) โ‰ค 50

Input Format:

First line  โ†’ Integer N
Next N lines โ†’ DNA fragments

Output Format:

Single integer โ†’ Minimum superstring length

Sample Input 1:

3
abcd
cdef
efgh

Sample Output 1:

8

Explanation: Superstring: abcdefgh length 8 (cd overlaps, ef overlaps) โœ…



โ“ Problem 94 โ€” The Segment Overlap Merge

๐Ÿท๏ธ Prime | Sorting/Intervals | Hard

Problem Statement: A video editing software has N timeline clips, each represented as an interval [start, end]. The software needs to merge all overlapping clips into the minimum number of non-overlapping intervals. Two intervals overlap if one starts before the other ends (or they touch at a boundary).

Constraints:

1 โ‰ค N โ‰ค 10^4
0 โ‰ค start โ‰ค end โ‰ค 10^4

Input Format:

First line  โ†’ Integer N
Next N lines โ†’ Two integers start end

Output Format:

Merged intervals, one per line: "start end"

Sample Input 1:

5
1 3
2 6
8 10
15 18
9 12

Sample Output 1:

1 6
8 12
15 18


โ“ Problem 95 โ€” The K-th Smallest in Matrix

๐Ÿท๏ธ Prime | Binary Search/Heap | Hard

Problem Statement: A satellite imaging system stores data in an Nร—N matrix where each row and column is sorted in ascending order. Given K, find the K-th smallest element in the matrix.

Constraints:

1 โ‰ค N โ‰ค 300
1 โ‰ค K โ‰ค Nยฒ
-10^9 โ‰ค matrix[i][j] โ‰ค 10^9

Input Format:

First line  โ†’ Two integers N K
Next N lines โ†’ N space-separated integers per row

Output Format:

Single integer โ†’ K-th smallest element

Sample Input 1:

3 8
1 5 9
10 11 13
12 13 15

Sample Output 1:

13


โ“ Problem 96 โ€” The 2-SAT Boolean Satisfiability

๐Ÿท๏ธ Prime | Graph/SCC | Very Hard

Problem Statement: A circuit designer has N boolean variables and M clauses, each of the form (xi OR xj) or (xi OR ยฌxj) or (ยฌxi OR ยฌxj). This is the 2-SAT Problem. Determine if there exists a valid assignment of True/False to all variables that satisfies all clauses. If yes, print "Satisfiable" and a valid assignment; else "Unsatisfiable". Solve using SCC + Topological Sort.

Constraints:

1 โ‰ค N โ‰ค 10^4
1 โ‰ค M โ‰ค 10^5

Input Format:

First line  โ†’ Two integers N M
Next M lines โ†’ Two integers i j
(positive = xi, negative = ยฌxi)

Output Format:

"Satisfiable" + assignment on next line, or "Unsatisfiable"

Sample Input 1:

2 3
1 2
-1 2
1 -2

Sample Output 1:

Satisfiable
x1=True x2=True


โ“ Problem 97 โ€” The Heavy-Light Decomposition

๐Ÿท๏ธ Prime | Tree/HLD | Very Hard

Problem Statement: A power grid is modelled as a tree of N nodes (power stations). Maintenance engineers make two types of queries repeatedly:

  1. UPDATE u v x โ†’ Add x to all nodes on the path from u to v
  2. QUERY u v โ†’ Find the maximum node value on the path from u to v

Use Heavy-Light Decomposition + Segment Tree for O(logยฒ n) per query.

Constraints:

1 โ‰ค N โ‰ค 10^5
1 โ‰ค Q โ‰ค 10^5
1 โ‰ค initial_value โ‰ค 10^6

Input Format:

First line  โ†’ Two integers N Q
Second line โ†’ N initial node values
Next (N-1) lines โ†’ Edges of the tree
Next Q lines โ†’ "UPDATE u v x" or "QUERY u v"

Output Format:

Answer for each QUERY on a new line

Sample Input 1:

5 3
1 2 3 4 5
1 2
1 3
2 4
2 5
QUERY 4 5
UPDATE 1 3 10
QUERY 1 3

Sample Output 1:

5
13


โ“ Problem 98 โ€” The Persistent Segment Tree

๐Ÿท๏ธ Prime | Persistent DS | Very Hard

Problem Statement: A version-controlled database needs to answer historical range minimum queries. The database has N initial values and supports:

Use a Persistent Segment Tree to store all versions efficiently.

Constraints:

1 โ‰ค N โ‰ค 10^5
1 โ‰ค Q โ‰ค 10^4

Input Format:

First line  โ†’ Two integers N Q
Second line โ†’ N initial values (version 0)
Next Q lines โ†’ "UPDATE i x" or "QUERY v l r"

Output Format:

Answer for each QUERY on a new line

Sample Input 1:

4 4
1 3 5 7
QUERY 0 1 4
UPDATE 2 10
QUERY 0 1 4
QUERY 1 1 4

Sample Output 1:

1
1
3


โ“ Problem 99 โ€” The Polynomial Multiplication via FFT

๐Ÿท๏ธ Prime | FFT/Math | Very Hard

Problem Statement: A signal processing system needs to multiply two large polynomials. Given polynomial A of degree N and polynomial B of degree M (both given as coefficient arrays), compute A ร— B using the Fast Fourier Transform (FFT) in O((N+M) log(N+M)). Print the coefficients of the resulting polynomial from degree 0 to degree N+M.

Constraints:

1 โ‰ค N, M โ‰ค 10^5
0 โ‰ค coefficient โ‰ค 100

Input Format:

First line  โ†’ Integer N (degree of A)
Second line โ†’ (N+1) space-separated integers (coefficients of A, from degree 0)
Third line  โ†’ Integer M (degree of B)
Fourth line โ†’ (M+1) space-separated integers (coefficients of B)

Output Format:

(N+M+1) space-separated integers โ†’ Coefficients of Aร—B

Sample Input 1:

2
1 2 1
2
1 1 1

Sample Output 1:

1 3 4 3 1

Explanation: (1 + 2x + xยฒ)(1 + x + xยฒ) = 1 + 3x + 4xยฒ + 3xยณ + xโด โœ…



โ“ Problem 100 โ€” The Final Boss: Offline LCA + Difference Array on Tree

๐Ÿท๏ธ Prime | Tree/LCA/Euler Tour | Very Hard

Problem Statement: A research institute models dependency relationships as a rooted tree (root = node 1) with N nodes. A professor issues Q queries, each of the form (u, v, x): add x to every node on the path from u to v. After all updates, print the final value of each node.

Use:

Constraints:

1 โ‰ค N โ‰ค 10^5
1 โ‰ค Q โ‰ค 10^5
1 โ‰ค x โ‰ค 10^4

Input Format:

First line  โ†’ Two integers N Q
Next (N-1) lines โ†’ Edges of the tree (undirected)
Next Q lines โ†’ Three integers u v x

Output Format:

N space-separated integers โ†’ Final value of each node (1 to N)

Sample Input 1:

5 2
1 2
1 3
2 4
2 5
1 4 5
3 5 3

Sample Output 1:

8 8 3 5 8

Explanation: Path 1โ†’4: nodes {1,2,4} += 5. Path 3โ†’5: nodes {3,1,2,5} += 3. Final: node1=8, node2=8, node3=3, node4=5, node5=8 โœ…



COMPLETE 100-QUESTION MASTER TABLE

Q# Level Type Difficulty
1โ€“35 ๐ŸŸข Ninja Basic Simulation, Patterns, Number Theory, Strings, Sorting Easy
36โ€“50 ๐Ÿ”ต Digital Linked Lists, Stacks, BFS/DFS, DP, Sorting, String Matching Medium
51โ€“70 ๐Ÿ”ต Digital Geometry, Backtracking, Trees, Graphs, Greedy, Trie, Sliding Window Mediumโ€“Hard
71โ€“85 ๐Ÿ”ด Prime Segment Tree, Fenwick Tree, LRU Cache, Max Flow, Advanced DP, Hashing Hardโ€“Very Hard
86โ€“100 ๐Ÿ”ด Prime Bitmask DP, FFT, Suffix Array, HLD, Persistent DS, 2-SAT, TSP Expert