0% found this document useful (0 votes)
8 views

TCS Coding Questions

Uploaded by

Saravana Kohli
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

TCS Coding Questions

Uploaded by

Saravana Kohli
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Mos t

Aske d TCS
Codi ng
By Prime Coding
Swap Two Numbers Without Using a Third Variable

ALGORITH
M
Let the numbers be a and b.

Use arithmetic operations to swap them:


a=a+b
b=a-b
a=a-b
Reverse a Given Number/String Using Command
Line Arguments

ALGORITH
M
• Read the string from command line arguments.
• Reverse the string using slicing or a loop.
• Print the reversed string.
Arrange 1s and 0s Together in a Single Array Scan

ALGORITH
M
• Initialize two pointers, left and right, at the beginning and end
of the array.
• Scan the array with two pointers.
• Swap elements when a 0 is encountered at right and a 1 is
encountered at left.
• Move pointers inward.
Function to Swap Two Numbers Without Using a
Temporary Variable

ALGORITH
M
Use bitwise XOR to swap the values.

def swap(a, b):


a=a^b
b=a^b
a=a^b
return a, b
Difference Between the Sum of Odd and Even
Position Digits

ALGORITH
M
• Initialize sum_odd and sum_even to 0.
• Iterate through each digit and add to the
corresponding sum based on the position.
Convert a Matrix into Lower Triangular Matrix

ALGORITH
M
Iterate over the matrix and set elements above the
main diagonal to 0.
Factorial of a Number Without Using Arithmetic
Operations

ALGORITH
M
Use recursive bit manipulation to compute the
factorial.
Number of Pairs Whose Average is Present in the
Array

ALGORITH
M
• Use a set to store unique elements.

• Iterate through each pair and check if their


average exists in the set.
Binary Search Algorithm

ALGORITH
M
• Initialize low to 0 and high to len(arr) - 1.
• While low is less than or equal to high:
• Calculate the mid index.
• If the target is equal to arr[mid], return the mid index.
• If the target is less than arr[mid], set high to mid - 1.
• If the target is greater than arr[mid], set low to mid + 1.
• If the target is not found, return -1.
Time & Space Complexity (Popular Sorting and
Searching Algorithms)

Sorting Algorithms:
• Bubble Sort: Time: O(n^2), Space: O(1)
• Selection Sort: Time: O(n^2), Space: O(1)
• Insertion Sort: Time: O(n^2), Space: O(1)
• Merge Sort: Time: O(n log n), Space: O(n)
• Quick Sort: Time: O(n log n) average, O(n^2) worst, Space: O(log n)
• Heap Sort: Time: O(n log n), Space: O(1)
• Radix Sort: Time: O(nk), Space: O(n + k) (where k is the range of the
input)
Time & Space Complexity (Popular Sorting and
Searching Algorithms)

Searching Algorithms:
• Linear Search: Time: O(n), Space: O(1)
• Binary Search: Time: O(log n), Space: O(1)
Unique Paths in a Grid

ALGORITH
M
• Use dynamic programming.
• Create a 2D array dp where dp[i][j] represents the
number of unique paths to cell (i, j).
• Initialize the first row and column to 1.
• Fill the rest of the dp table using the relation dp[i][j] =
dp[i-1][j] + dp[i][j-1].
Minimum and Maximum Element in Every
Contiguous Subarray of Size k

ALGORITH
M
• Use two deques to maintain the indices of minimum and
maximum elements.
• Slide the window of size k over the array and update the
deques.
• At each step, record the current minimum and maximum.
Rotate the Array by K Elements

ALGORITH
M
• Reverse the entire array.
• Reverse the first k elements.
• Reverse the remaining elements.
1:1 MOCK SESSIONS

Get confident
Real life Experience
Crack interviews Fearlessly

Starting at Rs. 99
Th ank
yo u!
Prime Coding

You might also like