Menu

Navigation

HomeAll CompaniesPrevious Year QuestionsTNP / PlacementsBEU ResultDocumentation

Companies

InfosysTCSWiproAccentureCognizantCapgemini
R's PlacePrep|
CompaniesPYQDocs

Company

Infosys

Content

2025 IRT2024 IRTAptitudeCoding

View Options

Display Mode

Sections

Quantitative Aptitude20
Logical Reasoning20
Verbal Ability20
Pseudo Code15
Coding Problems10

Export

PDF

Word

Last updated: Feb 11, 2026

Infosys

2024 IRT

Display Mode

Jump to Section

Quantitative Aptitude20
Logical Reasoning20
Verbal Ability20
Pseudo Code15
Coding Problems10

Other Content

2025 IRT2024 IRTAptitudeCoding

Export

PDF

Word

Home/Companies/Infosys/2024-irt
Back to Infosys

Infosys 2024-irt

5 sections, 85 questions

Quantitative Aptitude

20 questions
Question 1

Profit & Loss

Problem

A shopkeeper marks an item 30% above cost price and gives 10% discount. Find his profit percentage.

Solution

Let CP = 100
Marked Price = 130
Discount = 10% of 130 = 13
Selling Price = 130 - 13 = 117
Profit = 117 - 100 = 17
Profit % = (17/100) x 100 = 17%

Question 2

Time & Work

Problem

A and B can complete a work in 20 days and 30 days respectively. They work together for 6 days, then A leaves. In how many days will B complete the remaining work?

Solution

A's 1 day work = 1/20
B's 1 day work = 1/30
Together in 1 day = 1/20 + 1/30 = 5/60 = 1/12
Work done in 6 days = 6 x 1/12 = 1/2
Remaining work = 1 - 1/2 = 1/2
B will complete in = (1/2) / (1/30) = 15 days

Question 3

Percentage

Problem

If 20% of a number is 60, what is 35% of that number?

Solution

Let the number be x
20% of x = 60
x = 60 x 100/20 = 300
35% of 300 = 35/100 x 300 = 105

Question 4

Ratio & Proportion

Problem

The ratio of ages of A and B is 3:5. After 10 years, the ratio becomes 5:7. Find the present age of A.

Solution

Let present ages be 3x and 5x
After 10 years: (3x + 10)/(5x + 10) = 5/7
7(3x + 10) = 5(5x + 10)
21x + 70 = 25x + 50
4x = 20
x = 5
A's present age = 3 x 5 = 15 years

Question 5

Simple Interest

Problem

A sum of money becomes 2,400 in 2 years and 3,200 in 4 years at simple interest. Find the principal amount.

Solution

Interest for 2 years = 3200 - 2400 = 800
Interest per year = 400
Interest for 2 years = 800
Principal = 2400 - 800 = 1,600

Question 6

Compound Interest

Problem

Find the compound interest on 10,000 for 2 years at 10% per annum, compounded annually.

Solution

Amount = P(1 + R/100)^n
Amount = 10000(1 + 10/100)^2 = 10000 x 1.1^2 = 10000 x 1.21 = 12,100
CI = Amount - Principal = 12100 - 10000 = 2,100

Question 7

Time, Distance & Speed

Problem

A train 150 meters long passes a platform 200 meters long in 20 seconds. Find the speed of the train in km/hr.

Solution

Total distance = 150 + 200 = 350 meters
Time = 20 seconds
Speed = 350/20 = 17.5 m/s
Speed in km/hr = 17.5 x 18/5 = 63 km/hr

Question 8

Time, Distance & Speed - Complex

Problem

Abdul starts in a car from Ahmadabad towards Bangalore. After some time he realizes that he will cover only 75% of the distance in the scheduled time and he therefore doubles his speed immediately and thus manages to reach Bangalore exactly on time. Find the time after which Abdul changed his speed, given that he could have been late by 3 hours if he had not changed his speed.

Solution

Let total distance = D, scheduled time = T, original speed = V
Distance covered at original speed = 0.75D
Time before speed change = 0.75D/V = t
Remaining distance = 0.25D
New speed = 2V
Time for remaining = 0.25D/(2V) = 0.125D/V

Total time = t + 0.125D/V = T
If speed not changed: D/V = T + 3

Solving: 0.75D/V + 0.125D/V = D/V - 3
0.875D/V = D/V - 3
0.125D/V = 3
D/V = 24, T = 21
Time before change = 0.75 x 24 = 18 hours

Question 9

Permutations & Combinations

Problem

A code word is to consist of two English alphabets followed by two distinct numbers between 1 and 9. For example, CA23 is a code word. How many such code words are there?

Solution

Alphabets: 26 x 26 = 676 ways
Numbers: 9 x 8 = 72 ways (distinct)
Total: 676 x 72 = 48,672

Question 10

Pipes & Cisterns

Problem

There are two pipes in a tank. Pipe A is for filling the tank and pipe B is for emptying the tank. If A can fill the tank in 10 hours and B can empty the tank in 15 hours then find how many hours will it take to completely fill a half empty tank?

Solution

Net rate = 1/10 - 1/15 = 1/30 per hour
Time to fill half tank = (1/2) / (1/30) = 15 hours

Question 11

Probability

Problem

Two dice are thrown. What is the probability of getting a sum of 7?

Solution

Total outcomes = 6 x 6 = 36
Favorable outcomes: (1,6), (2,5), (3,4), (4,3), (5,2), (6,1) = 6
Probability = 6/36 = 1/6

Question 12

Mixtures & Alligations

Problem

In what ratio should water be mixed with milk costing 12 per liter to get a mixture worth 8 per liter?

Solution

Using alligation:
Water (0) and Milk (12) to get mixture of 8
Ratio = (12-8):(8-0) = 4:8 = 1:2

Question 13

Average

Problem

The average of 5 numbers is 25. If one number is excluded, the average becomes 23. Find the excluded number.

Solution

Sum of 5 numbers = 5 x 25 = 125
Sum of 4 numbers = 4 x 23 = 92
Excluded number = 125 - 92 = 33

Question 14

Number System

Problem

Find the remainder when 2^31 is divided by 5.

Solution

Pattern of 2^n mod 5: 2^1=2, 2^2=4, 2^3=3, 2^4=1, 2^5=2 (cycle repeats every 4)
31 mod 4 = 3
So 2^31 mod 5 = 2^3 mod 5 = 3

Question 15

Data Interpretation

Problem

The following table shows the sales of a company over 5 years. If the total sales in 2020 was 50,000, and sales increased by 20% each year, find the sales in 2024.

Solution

Sales in 2020 = 50,000
Sales in 2021 = 50,000 x 1.2 = 60,000
Sales in 2022 = 60,000 x 1.2 = 72,000
Sales in 2023 = 72,000 x 1.2 = 86,400
Sales in 2024 = 86,400 x 1.2 = 103,680

Question 16

Ages

Problem

The sum of ages of 5 children born at intervals of 3 years is 50 years. Find the age of the youngest child.

Solution

Let youngest child's age = x
Ages: x, x+3, x+6, x+9, x+12
Sum = 5x + 30 = 50
5x = 20
x = 4 years

Question 17

Partnership

Problem

A and B invest 5,000 and 7,000 respectively in a business. After 6 months, A withdraws 2,000. If the profit at the end of the year is 4,500, find B's share.

Solution

A's investment: 5000 x 6 + 3000 x 6 = 30,000 + 18,000 = 48,000
B's investment: 7000 x 12 = 84,000
Ratio = 48,000 : 84,000 = 4 : 7
B's share = 7/(4+7) x 4500 = 7/11 x 4500 = 2,863.64

Question 18

Boats & Streams

Problem

A man can row 8 km/hr in still water. If the speed of the stream is 2 km/hr, find the time taken to row 30 km downstream.

Solution

Downstream speed = 8 + 2 = 10 km/hr
Time = Distance/Speed = 30/10 = 3 hours

Question 19

Trains

Problem

Two trains of lengths 120m and 180m are running in opposite directions at speeds of 40 km/hr and 50 km/hr respectively. Find the time taken to cross each other.

Solution

Relative speed = 40 + 50 = 90 km/hr = 90 x 5/18 = 25 m/s
Total distance = 120 + 180 = 300m
Time = 300/25 = 12 seconds

Question 20

Area & Volume

Problem

The length and breadth of a rectangle are increased by 20% and 10% respectively. Find the percentage increase in area.

Solution

Let original length = L, breadth = B
Original area = L x B
New length = 1.2L, new breadth = 1.1B
New area = 1.2L x 1.1B = 1.32LB
Increase = 1.32LB - LB = 0.32LB
Percentage increase = (0.32LB/LB) x 100 = 32%

Logical Reasoning

20 questions
Question 1

Number Series

Problem

Find the next number: 2, 5, 11, 23, 47, ?

Solution

Pattern: Each number = previous x 2 + 1
2x2+1=5, 5x2+1=11, 11x2+1=23, 23x2+1=47
Next: 47x2+1 = 95

Question 2

Number Series - Alternate Pattern

Problem

Find the next number: 3, 8, 15, 24, 35, ?

Solution

Pattern: Differences are 5, 7, 9, 11 (odd numbers)
Next difference = 13
Next number = 35 + 13 = 48

Question 3

Letter Series

Problem

Find the next letter: A, D, G, J, ?

Solution

Pattern: A(+3)->D(+3)->G(+3)->J(+3)->M
Next letter = M

Question 4

Coding-Decoding

Problem

If "TECHNOLOGY" is coded as "VGEPQMQNA", how is "COMPUTER" coded?

Solution

Pattern: Each letter shifted by +2
T->V, E->G, C->E, H->J, N->P, O->Q, L->N, O->Q, G->I, Y->A

COMPUTER: C->E, O->Q, M->O, P->R, U->W, T->V, E->G, R->T

Question 5

Coding-Decoding - Number Pattern

Problem

If "APPLE" is coded as "1225165", how is "MANGO" coded?

Solution

Pattern: A=1, B=2, C=3... Z=26
APPLE: A=1, P=16, P=16, L=12, E=5 -> 1225165
MANGO: M=13, A=1, N=14, G=7, O=15 -> 13114715

Question 6

Syllogism

Problem

Statements: 1. All green are blue, 2. All blue are white

Conclusions: I) Some blue are green, II) Some white are green, III) Some green are white, IV) All white are blue.

Solution

From "All green are blue": Some blue are green (I follows)
Combined: All green are white, so Some white are green (II follows) and Some green are white (III follows)
IV does not follow

Question 7

Syllogism - Multiple Statements

Problem

Statements: 1. Some cats are dogs, 2. All dogs are animals, 3. No animal is a bird

Conclusions: I) Some cats are animals, II) No dog is a bird, III) Some animals are cats

Solution

From statements 1 and 2: Some cats are animals (I follows)
From statements 2 and 3: No dog is a bird (II follows)
From I: Some animals are cats (III follows)

Question 8

Blood Relations

Problem

Pointing to a man, a woman said, "His mother is the only daughter of my mother." How is the woman related to the man?

Solution

Only daughter of woman's mother = woman herself
So woman is the mother of the man

Question 9

Blood Relations - Complex

Problem

A is the son of B. C is the daughter of A. E is the son of B. How is E related to C?

Solution

A is son of B, so B is parent of A
C is daughter of A, so C is grandchild of B
E is son of B, so E is child of B
Therefore, E is uncle/aunt of C (depending on gender)

Question 10

Direction Sense

Problem

A person walks 5 km north, then 3 km east, then 2 km south. How far and in which direction is he from the starting point?

Solution

Net north = 5 - 2 = 3 km
Net east = 3 km
Distance = sqrt(3^2 + 3^2) = sqrt(18) = 3sqrt(2) km
Direction = Northeast

Question 11

Seating Arrangement - Linear

Problem

Five friends A, B, C, D, E are sitting in a row. A is not at either end. B is to the immediate right of A. C is at one end. D is between C and E. Who is sitting in the middle?

Solution

C is at one end. D is between C and E, so arrangement: C-D-E or E-D-C
A is not at end, B is right of A
If C-D-E, then A-B must be before C: A-B-C-D-E (A at end, invalid)
So: E-D-C, and A-B before: A-B-E-D-C
Middle position: E

Question 12

Seating Arrangement - Circular

Problem

Six people A, B, C, D, E, F are sitting around a circular table. A is opposite to D. B is between A and C. E is not adjacent to A. Who is sitting opposite to E?

Solution

In circular arrangement with 6 people, opposite pairs are: (A,D), (B,E), (C,F)
Since A is opposite D, and B is between A and C, and E is not adjacent to A
E must be opposite B

Question 13

Ordering & Ranking

Problem

In a class of 40 students, Ravi ranks 15th from the top. What is his rank from the bottom?

Solution

Total students = 40
Rank from top = 15
Rank from bottom = 40 - 15 + 1 = 26

Question 14

Cube Problem

Problem

A cube, painted yellow on all faces is cut into 27 small cubes of equal size. How many small cubes are painted on one face only?

Solution

3x3x3 cube arrangement
Cubes with 1 face painted = 6 (one on each face center)

Question 15

Cube Problem - Three Colors

Problem

A cube is painted red on three adjacent faces, blue on two opposite faces, and left unpainted on one face. It is cut into 64 smaller cubes. How many cubes have exactly two painted faces?

Solution

4x4x4 cube = 64 cubes
Cubes with exactly 2 painted faces are on edges (excluding corners)
Need to count edges with two colors

Question 16

Statement & Conclusions

Problem

Statement: All roses are flowers. Some flowers are red.

Conclusions: I) Some roses are red, II) All red things are flowers

Solution

From "All roses are flowers" and "Some flowers are red", we cannot conclude that some roses are red (I may not follow)
"All red things are flowers" is not necessarily true (II does not follow)

Question 17

Data Sufficiency

Problem

What is the value of x?

Statement 1: x^2 = 16
Statement 2: x > 0

Solution

From statement 1: x = 4 or -4
From statement 2: x > 0
Combining both: x = 4

Question 18

Logical Deduction

Problem

If all managers are leaders, and some leaders are innovators, which of the following must be true?

a) All managers are innovators
b) Some managers are innovators
c) Some innovators are managers
d) None of the above

Solution

From "All managers are leaders" and "Some leaders are innovators", we can conclude that "Some innovators are managers" (c)

Question 19

Pattern Recognition

Problem

Find the missing number: 2, 6, 12, 20, 30, ?

Solution

Pattern: Differences are 4, 6, 8, 10 (increasing by 2)
Next difference = 12
Next number = 30 + 12 = 42

Question 20

Crypt Arithmetic

Problem

If WAIT + ALL = GIFTS, then what is the value of G+I+F+T? (Given A=6 & S=5)

Solution

This is a crypt arithmetic problem where letters represent digits. Given A=6 and S=5, we need to solve the addition column by column to find values for other letters.

Verbal Ability

20 questions
Question 1

Reading Comprehension

Problem

Passage: "Artificial Intelligence has revolutionized various industries. Machine learning algorithms can now process vast amounts of data to identify patterns and make predictions. However, concerns about job displacement and ethical implications remain significant challenges."

Question: According to the passage, what are the main concerns about AI?

Solution

The passage mentions two main concerns: job displacement and ethical implications.

Question 2

Grammar - Subject-Verb Agreement

Problem

Choose the correct sentence:

a) Neither the students nor the teacher were present
b) Neither the students nor the teacher was present
c) Neither the students or the teacher was present

Solution

With "neither...nor", the verb agrees with the subject closer to it. "Teacher" is singular, so "was" is correct.

Question 3

Grammar - Tenses

Problem

Choose the correct form: "I _____ to the store yesterday."

a) go
b) went
c) have gone
d) will go

Solution

"Yesterday" indicates past tense, so "went" is correct.

Question 4

Vocabulary - Synonyms

Problem

Find the synonym of "ABUNDANT":

a) Scarce
b) Plentiful
c) Limited
d) Rare

Solution

Abundant means existing in large quantities, so "Plentiful" is the synonym.

Question 5

Vocabulary - Antonyms

Problem

Find the antonym of "TRANSPARENT":

a) Clear
b) Opaque
c) Visible
d) Bright

Solution

Transparent means see-through, so "Opaque" (not see-through) is the antonym.

Question 6

Sentence Correction

Problem

Identify the error: "The committee have decided to postpone the meeting."

Solution

"Committee" is a collective noun and takes singular verb. Should be "has decided".

Question 7

Fill in the Blanks

Problem

The scientist's _____ discovery revolutionized the field of medicine.

a) groundbreaking
b) trivial
c) ordinary
d) minor

Solution

A discovery that revolutionized a field would be "groundbreaking" (innovative and pioneering).

Question 8

Para Jumbles

Problem

Arrange the following sentences in correct order:
P. He was born in a small village.
Q. Today, he is one of the most successful entrepreneurs.
R. He started his first business at the age of 16.
S. Despite many failures, he never gave up.

Solution

Logical order: Birth -> First business -> Failures -> Success
P -> R -> S -> Q

Question 9

Idioms and Phrases

Problem

What does "break the ice" mean?

a) To destroy something
b) To initiate conversation
c) To cause problems
d) To solve a mystery

Solution

"Break the ice" means to initiate conversation or relieve tension in a social situation.

Question 10

One Word Substitution

Problem

A person who speaks many languages is called:

a) Bilingual
b) Polyglot
c) Linguist
d) Translator

Solution

A polyglot is someone who speaks or uses several languages.

Question 11

Spelling Correction

Problem

Identify the correctly spelled word:

a) Accomodation
b) Accommodation
c) Acommodation
d) Acomodation

Solution

The correct spelling has double 'c' and double 'm'.

Question 12

Active and Passive Voice

Problem

Convert to passive voice: "The manager approved the project."

Solution

Subject becomes object, object becomes subject, verb changes to past participle.

Question 13

Direct and Indirect Speech

Problem

Convert to indirect speech: He said, "I am going to the market."

Solution

Change tense and pronouns appropriately.

Question 14

Cloze Test

Problem

Fill in the blank: The rapid _____ of technology has transformed how we communicate.

a) advancement
b) decline
c) stagnation
d) retreat

Solution

Technology has been advancing, leading to transformation. "Advancement" fits best.

Question 15

Reading Comprehension - Inference

Problem

Based on the context "The CEO's sudden resignation sent shockwaves through the company, causing stock prices to plummet by 15%.", what can be inferred?

Solution

The CEO's departure was unexpected and had a significant negative impact on investor confidence.

Question 16

Synonyms in Context

Problem

In the sentence "The teacher was lauded for her innovative teaching methods", the word "lauded" means:

a) criticized
b) praised
c) ignored
d) questioned

Solution

"Lauded" means praised or applauded.

Question 17

Sentence Rearrangement

Problem

Rearrange to form a meaningful sentence: "success / hard work / the / of / key / is / to"

Solution

Arrange subject, verb, and objects in proper order.

Question 18

Error Spotting

Problem

Find the error: "Each of the students have submitted their assignments."

Solution

"Each" is singular and requires singular verb. Should be "has submitted".

Question 19

Comprehension - Main Idea

Problem

"Climate change is causing sea levels to rise, threatening coastal cities worldwide. Scientists predict that by 2100, many low-lying areas could be submerged." What is the main idea?

Solution

The main idea is about the threat of rising sea levels due to climate change.

Question 20

Vocabulary - Context Clues

Problem

"Despite the team's best efforts, the project remained in a state of limbo, with no clear decision from management." What does "limbo" mean here?

a) success
b) uncertainty
c) completion
d) rejection

Solution

In context, "limbo" refers to a state of uncertainty or waiting.

Pseudo Code

15 questions
Question 1

Variable Tracing

Problem

What is the output of the following code?

int a = 5, b = 3;
a = a + b;
b = a - b;
a = a - b;
print(a, b);

Solution

Initial: a = 5, b = 3
After a = a + b: a = 8, b = 3
After b = a - b: a = 8, b = 5
After a = a - b: a = 3, b = 5

This is the swapping algorithm without temporary variable.

Question 2

Loop Output

Problem

What is the output?

int sum = 0;
for(int i = 1; i <= 5; i++) {
    if(i % 2 == 0)
        sum = sum + i;
}
print(sum);

Solution

Loop runs for i = 1, 2, 3, 4, 5
Even numbers: 2, 4
Sum = 2 + 4 = 6

Question 3

Nested Loop

Problem

How many times will "Hello" be printed?

for(int i = 0; i < 3; i++) {
    for(int j = 0; j <= i; j++) {
        print("Hello");
    }
}

Solution

i=0: j=0 (1 time)
i=1: j=0,1 (2 times)
i=2: j=0,1,2 (3 times)
Total: 1 + 2 + 3 = 6 times

Question 4

Recursion Output

Problem

What is the output?

int fun(int n) {
    if(n <= 0)
        return 0;
    return n + fun(n-2);
}
print(fun(5));

Solution

fun(5) = 5 + fun(3)
fun(3) = 3 + fun(1)
fun(1) = 1 + fun(-1)
fun(-1) = 0

Total: 5 + 3 + 1 + 0 = 9

Question 5

Array Manipulation

Problem

What is the output?

int arr[] = {1, 2, 3, 4, 5};
int n = 5;
for(int i = 0; i < n/2; i++) {
    int temp = arr[i];
    arr[i] = arr[n-1-i];
    arr[n-1-i] = temp;
}
print(arr[2]);

Solution

This reverses the array.
Original: {1, 2, 3, 4, 5}
After swap (i=0): {5, 2, 3, 4, 1}
After swap (i=1): {5, 4, 3, 2, 1}
arr[2] = 3 (middle element unchanged)

Question 6

Conditional Statements

Problem

What is the output?

int x = 10, y = 20, z = 30;
if(x > y && y > z)
    print("A");
else if(x < y || y > z)
    print("B");
else
    print("C");

Solution

x > y && y > z: 10 > 20 && 20 > 30 = false && false = false
x < y || y > z: 10 < 20 || 20 > 30 = true || false = true

Question 7

String Operations

Problem

What is the output?

String s = "HELLO";
String result = "";
for(int i = s.length()-1; i >= 0; i--) {
    result = result + s[i];
}
print(result);

Solution

This reverses the string character by character.
HELLO -> OLLEH

Question 8

Bitwise Operations

Problem

What is the output?

int a = 5, b = 3;
print(a & b);
print(a | b);
print(a ^ b);

Solution

5 in binary: 101
3 in binary: 011

a & b (AND): 101 & 011 = 001 = 1
a | b (OR): 101 | 011 = 111 = 7
a ^ b (XOR): 101 ^ 011 = 110 = 6

Question 9

Function Call

Problem

What is the output?

int multiply(int a, int b) {
    return a * b;
}

int add(int a, int b) {
    return a + b;
}

print(add(multiply(2, 3), multiply(3, 4)));

Solution

multiply(2, 3) = 6
multiply(3, 4) = 12
add(6, 12) = 18

Question 10

While Loop

Problem

What is the output?

int n = 12345;
int count = 0;
while(n > 0) {
    count++;
    n = n / 10;
}
print(count);

Solution

This counts the digits in the number.
12345 -> 1234 -> 123 -> 12 -> 1 -> 0
Count: 1, 2, 3, 4, 5 = 5 digits

Question 11

Switch Case

Problem

What is the output?

int x = 2;
switch(x) {
    case 1: print("A");
    case 2: print("B");
    case 3: print("C");
    default: print("D");
}

Solution

Without break statements, execution falls through all cases after matching.
x = 2 matches case 2, then falls through to case 3 and default.

Question 12

Pointer/Reference

Problem

What is the output?

int a = 10;
int b = a;
a = 20;
print(b);

Solution

b is assigned the value of a (10).
Changing a to 20 doesn't affect b since b has its own copy.

Question 13

Matrix Operations

Problem

What is the sum of diagonal elements?

int mat[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int sum = 0;
for(int i = 0; i < 3; i++) {
    sum = sum + mat[i][i];
}
print(sum);

Solution

Diagonal elements: mat[0][0]=1, mat[1][1]=5, mat[2][2]=9
Sum = 1 + 5 + 9 = 15

Question 14

Do-While Loop

Problem

What is the output?

int i = 5;
do {
    print(i);
    i--;
} while(i > 5);

Solution

Do-while executes at least once.
First iteration: prints 5, i becomes 4
Condition 4 > 5 is false, loop ends.

Question 15

Increment/Decrement

Problem

What is the output?

int a = 5;
int b = ++a + a++;
print(a, b);

Solution

++a: a becomes 6, value used is 6
a++: value used is 6, then a becomes 7
b = 6 + 6 = 12
Final: a = 7, b = 12

Coding Problems

10 questions
Question 1

Find Second Largest

Problem

Find the second largest element in an array.

Solution

Iterate through the array once, maintaining two variables for the first and second largest elements. Update them as we find larger elements.




Code

C
#include <stdio.h>
#include <limits.h>

int secondLargest(int arr[], int n) {
    int first = INT_MIN, second = INT_MIN;
    for (int i = 0; i < n; i++) {
        if (arr[i] > first) {
            second = first;
            first = arr[i];
        } else if (arr[i] > second && arr[i] != first) {
            second = arr[i];
        }
    }
    return second;
}
Java
public int secondLargest(int[] arr) {
    int first = Integer.MIN_VALUE;
    int second = Integer.MIN_VALUE;
    for (int num : arr) {
        if (num > first) {
            second = first;
            first = num;
        } else if (num > second && num != first) {
            second = num;
        }
    }
    return second;
}
Python
def second_largest(arr):
    first = second = float('-inf')
    for num in arr:
        if num > first:
            second = first
            first = num
        elif num > second and num != first:
            second = num
    return second
Time: O(n)Space: O(1)
Question 2

Reverse Array

Problem

Reverse an array without using extra space.

Solution

Use two pointers approach - one at start and one at end. Swap elements and move pointers towards center.




Code

C
void reverseArray(int arr[], int n) {
    int start = 0, end = n - 1;
    while (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}
Java
public void reverseArray(int[] arr) {
    int start = 0, end = arr.length - 1;
    while (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}
Python
def reverse_array(arr):
    start, end = 0, len(arr) - 1
    while start < end:
        arr[start], arr[end] = arr[end], arr[start]
        start += 1
        end -= 1
Time: O(n)Space: O(1)
Question 3

Check Palindrome String

Problem

Check if a string is palindrome (case-insensitive).

Solution

Compare characters from both ends, ignoring case. If all match, it's a palindrome.




Code

C
#include <ctype.h>
#include <string.h>

int isPalindrome(char str[]) {
    int left = 0, right = strlen(str) - 1;
    while (left < right) {
        if (tolower(str[left]) != tolower(str[right]))
            return 0;
        left++;
        right--;
    }
    return 1;
}
Java
public boolean isPalindrome(String str) {
    str = str.toLowerCase();
    int left = 0, right = str.length() - 1;
    while (left < right) {
        if (str.charAt(left) != str.charAt(right))
            return false;
        left++;
        right--;
    }
    return true;
}
Python
def is_palindrome(s):
    s = s.lower()
    left, right = 0, len(s) - 1
    while left < right:
        if s[left] != s[right]:
            return False
        left += 1
        right -= 1
    return True
Time: O(n)Space: O(1)
Question 4

Prime Number Check

Problem

Check if a number is prime.

Solution

Check divisibility from 2 to sqrt(n). If no divisor found, number is prime.




Code

C
int isPrime(int n) {
    if (n <= 1) return 0;
    if (n <= 3) return 1;
    if (n % 2 == 0 || n % 3 == 0) return 0;
    for (int i = 5; i * i <= n; i += 6) {
        if (n % i == 0 || n % (i + 2) == 0)
            return 0;
    }
    return 1;
}
Java
public boolean isPrime(int n) {
    if (n <= 1) return false;
    if (n <= 3) return true;
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (int i = 5; i * i <= n; i += 6) {
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    }
    return true;
}
Python
def is_prime(n):
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    i = 5
    while i * i <= n:
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i += 6
    return True
Time: O(sqrt(n))Space: O(1)
Question 5

GCD of Two Numbers

Problem

Find GCD (Greatest Common Divisor) of two numbers using Euclidean algorithm.

Solution

Use recursive approach: GCD(a, b) = GCD(b, a % b), base case when b = 0.




Code

C
int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
Java
public int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
Python
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)
Time: O(log(min(a,b)))Space: O(log(min(a,b)))
Question 6

Fibonacci Series

Problem

Print Fibonacci series up to n terms.

Solution

Start with 0 and 1, each subsequent number is sum of previous two.




Code

C
void fibonacci(int n) {
    if (n <= 0) return;
    int a = 0, b = 1;
    printf("%d ", a);
    if (n > 1) printf("%d ", b);
    for (int i = 2; i < n; i++) {
        int c = a + b;
        printf("%d ", c);
        a = b;
        b = c;
    }
}
Java
public void fibonacci(int n) {
    if (n <= 0) return;
    int a = 0, b = 1;
    System.out.print(a + " ");
    if (n > 1) System.out.print(b + " ");
    for (int i = 2; i < n; i++) {
        int c = a + b;
        System.out.print(c + " ");
        a = b;
        b = c;
    }
}
Python
def fibonacci(n):
    if n <= 0:
        return
    a, b = 0, 1
    print(a, end=" ")
    if n > 1:
        print(b, end=" ")
    for i in range(2, n):
        c = a + b
        print(c, end=" ")
        a, b = b, c
Time: O(n)Space: O(1)
Question 7

Maximum Subarray Sum (Kadane's Algorithm)

Problem

Find maximum sum of contiguous subarray.

Solution

Kadane's algorithm: Track current sum and max sum. Reset current sum if it becomes negative.




Code

C
int maxSubArraySum(int arr[], int n) {
    int max_sum = arr[0];
    int current_sum = arr[0];
    for (int i = 1; i < n; i++) {
        current_sum = (arr[i] > current_sum + arr[i]) ? arr[i] : current_sum + arr[i];
        max_sum = (current_sum > max_sum) ? current_sum : max_sum;
    }
    return max_sum;
}
Java
public int maxSubArraySum(int[] arr) {
    int maxSum = arr[0];
    int currentSum = arr[0];
    for (int i = 1; i < arr.length; i++) {
        currentSum = Math.max(arr[i], currentSum + arr[i]);
        maxSum = Math.max(maxSum, currentSum);
    }
    return maxSum;
}
Python
def max_subarray_sum(arr):
    max_sum = current_sum = arr[0]
    for i in range(1, len(arr)):
        current_sum = max(arr[i], current_sum + arr[i])
        max_sum = max(max_sum, current_sum)
    return max_sum
Time: O(n)Space: O(1)
Question 8

Two Sum

Problem

Find two numbers that add up to target.

Solution

Use hash map to store complement of each number. Check if complement exists before adding.



Code

Java
public int[] twoSum(int[] arr, int target) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < arr.length; i++) {
        int complement = target - arr[i];
        if (map.containsKey(complement)) {
            return new int[]{map.get(complement), i};
        }
        map.put(arr[i], i);
    }
    return new int[]{};
}
Python
def two_sum(arr, target):
    seen = {}
    for i, num in enumerate(arr):
        complement = target - num
        if complement in seen:
            return [seen[complement], i]
        seen[num] = i
    return []
Time: O(n)Space: O(n)
Question 9

Check Anagram

Problem

Check if two strings are anagrams.

Solution

Sort both strings and compare, or use character frequency count.



Code

Java
public boolean isAnagram(String s1, String s2) {
    if (s1.length() != s2.length()) return false;
    char[] c1 = s1.toCharArray();
    char[] c2 = s2.toCharArray();
    Arrays.sort(c1);
    Arrays.sort(c2);
    return Arrays.equals(c1, c2);
}
Python
def is_anagram(s1, s2):
    if len(s1) != len(s2):
        return False
    return sorted(s1) == sorted(s2)
Time: O(n log n)Space: O(1)
Question 10

Rotate Array

Problem

Rotate array to right by k positions.

Solution

Reverse entire array, then reverse first k elements, then reverse remaining elements.



Code

Java
public void rotate(int[] arr, int k) {
    k = k % arr.length;
    reverse(arr, 0, arr.length - 1);
    reverse(arr, 0, k - 1);
    reverse(arr, k, arr.length - 1);
}

private void reverse(int[] arr, int start, int end) {
    while (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}
Python
def rotate(arr, k):
    k = k % len(arr)
    arr.reverse()
    arr[:k] = reversed(arr[:k])
    arr[k:] = reversed(arr[k:])
Time: O(n)Space: O(1)

Last updated: 2026-02-11

Radhika's PlacePrep built by Rohan Sharma