Skip to main content

Command Palette

Search for a command to run...

Top 10 tcs Coding Interview Questions from 2025

Updated
6 min read

Introduction

In this blog post, we'll share the most commonly asked coding interview questions at tcs. If you don't have months to study for your interviews, you can use AI tools like Chatmagic to generate solutions quickly and efficiently - helping you pass the interviews and get the job offer!

Problem #2: Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. Example 2: Input: nums = [3,2,4], target = 6 Output: [1,2] Example 3: Input: nums = [3,3], target = 6 Output: [0,1] Constraints: 2 <= nums.length <= 104 -109 <= nums[i] <= 109 -109 <= target <= 109 Only one valid answer exists. Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?

Topics: Array, Hash Table

Problem #3: Zigzag Grid Traversal With Skip

You are given an m x n 2D array grid of positive integers. Your task is to traverse grid in a zigzag pattern while skipping every alternate cell. Zigzag pattern traversal is defined as following the below actions: Start at the top-left cell (0, 0). Move right within a row until the end of the row is reached. Drop down to the next row, then traverse left until the beginning of the row is reached. Continue alternating between right and left traversal until every row has been traversed. Note that you must skip every alternate cell during the traversal. Return an array of integers result containing, in order, the value of the cells visited during the zigzag traversal with skips. Example 1: Input: grid = [[1,2],[3,4]] Output: [1,4] Explanation: Example 2: Input: grid = [[2,1],[2,1],[2,1]] Output: [2,1,2] Explanation: Example 3: Input: grid = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,3,5,7,9] Explanation: Constraints: 2 <= n == grid.length <= 50 2 <= m == grid[i].length <= 50 1 <= grid[i][j] <= 2500

Topics: Array, Matrix, Simulation

Problem #4: Palindrome Number

Given an integer x, return true if x is a palindrome, and false otherwise. Example 1: Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left. Example 2: Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Constraints: -231 <= x <= 231 - 1 Follow up: Could you solve it without converting the integer to a string?

Topics: Math

Problem #5: Jump Game

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or false otherwise. Example 1: Input: nums = [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: nums = [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index. Constraints: 1 <= nums.length <= 104 0 <= nums[i] <= 105

Topics: Array, Dynamic Programming, Greedy

Problem #6: Majority Element

Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. Example 1: Input: nums = [3,2,3] Output: 3 Example 2: Input: nums = [2,2,1,1,1,2,2] Output: 2 Constraints: n == nums.length 1 <= n <= 5 * 104 -109 <= nums[i] <= 109 Follow-up: Could you solve the problem in linear time and in O(1) space?

Topics: Array, Hash Table, Divide and Conquer, Sorting, Counting

Problem #7: Split Array With Same Average

You are given an integer array nums. You should move each element of nums into one of the two arrays A and B such that A and B are non-empty, and average(A) == average(B). Return true if it is possible to achieve that and false otherwise. Note that for an array arr, average(arr) is the sum of all the elements of arr over the length of arr. Example 1: Input: nums = [1,2,3,4,5,6,7,8] Output: true Explanation: We can split the array into [1,4,5,8] and [2,3,6,7], and both of them have an average of 4.5. Example 2: Input: nums = [3,1] Output: false Constraints: 1 <= nums.length <= 30 0 <= nums[i] <= 104

Topics: Array, Math, Dynamic Programming, Bit Manipulation, Bitmask

Problem #8: Count Number of Pairs With Absolute Difference K

Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k. The value of |x| is defined as: x if x >= 0. -x if x < 0. Example 1: Input: nums = [1,2,2,1], k = 1 Output: 4 Explanation: The pairs with an absolute difference of 1 are: - [1,2,2,1] - [1,2,2,1] - [1,2,2,1] - [1,2,2,1] Example 2: Input: nums = [1,3], k = 3 Output: 0 Explanation: There are no pairs with an absolute difference of 3. Example 3: Input: nums = [3,2,1,5,4], k = 2 Output: 3 Explanation: The pairs with an absolute difference of 2 are: - [3,2,1,5,4] - [3,2,1,5,4] - [3,2,1,5,4] Constraints: 1 <= nums.length <= 200 1 <= nums[i] <= 100 1 <= k <= 99

Topics: Array, Hash Table, Counting

Problem #9: Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: strs = ["flower","flow","flight"] Output: "fl" Example 2: Input: strs = ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. Constraints: 1 <= strs.length <= 200 0 <= strs[i].length <= 200 strs[i] consists of only lowercase English letters if it is non-empty.

Topics: String, Trie

Problem #10: Find the Index of the First Occurrence in a String

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "sadbutsad", needle = "sad" Output: 0 Explanation: "sad" occurs at index 0 and 6. The first occurrence is at index 0, so we return 0. Example 2: Input: haystack = "leetcode", needle = "leeto" Output: -1 Explanation: "leeto" did not occur in "leetcode", so we return -1. Constraints: 1 <= haystack.length, needle.length <= 104 haystack and needle consist of only lowercase English characters.

Topics: Two Pointers, String, String Matching

More from this blog

C

Chatmagic blog

2894 posts