give me coading round questions of infosys
- User
give me coading round questions of infosys
- Assistant
- User
hardest problems
- Assistant
give me coading round questions of infosys
hardest problems
Certainly! Infosys coding round questions often focus on data structures, algorithms, and problem-solving skills. Here are some commonly asked types of questions along with a few sample problems that can help you prepare:
You are given an array of size n-1 containing distinct integers in the range [1, n]. Find the missing number.
def find_missing_number(arr, n):
total = n * (n + 1) // 2
return total - sum(arr)
# Example:
arr = [1, 2, 4, 6, 3, 7, 8]
n = 8
print(find_missing_number(arr, n)) # Output: 5
Write a function to reverse a string.
def reverse_string(s):
return s[::-1]
# Example:
print(reverse_string("Infosys")) # Output: sysofni
Determine if a number is a palindrome (reads the same backward and forward).
def is_palindrome(num):
return str(num) == str(num)[::-1]
# Example:
print(is_palindrome(121)) # Output: True
print(is_palindrome(123)) # Output: False
Generate the first n Fibonacci numbers.
def fibonacci(n):
a, b = 0, 1
fib_series = []
for _ in range(n):
fib_series.append(a)
a, b = b, a+b
return fib_series
# Example:
print(fibonacci(7)) # Output: [0, 1, 1, 2, 3, 5, 8]
Find the maximum element in an array.
def find_largest(arr):
return max(arr)
# Example:
print(find_largest([3, 9, 2, 5, 7])) # Output: 9
Write a program to check if two strings are anagrams.
def are_anagrams(s1, s2):
return sorted(s1) == sorted(s2)
# Example:
print(are_anagrams("listen", "silent")) # Output: True
print(are_anagrams("hello", "world")) # Output: False
If you'd like, I can also provide medium or hard level questions or help with explanations for any particular topics!
Sure! Below are some hard-level coding problems that are great practice for cracking tough rounds at Infosys or similar companies. These problems test deeper algorithmic skills like advanced dynamic programming, graph theory, and tricky data structure manipulations.
Find the length of the longest strictly increasing subsequence in an array.
def length_of_LIS(nums):
if not nums:
return 0
dp = [1] * len(nums) # dp[i] = length of LIS ending at i
for i in range(1, len(nums)):
for j in range(i):
if nums[j] < nums[i]:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
# Example:
print(length_of_LIS([10, 9, 2, 5, 3, 7, 101, 18])) # Output: 4 [2,3,7,101]
Given a string and a dictionary of words, determine if the string can be segmented into a space-separated sequence of one or more dictionary words.
def word_break(s, wordDict):
dp = [False] * (len(s) + 1)
dp[0] = True
for i in range(1, len(s) + 1):
for j in range(i):
if dp[j] and s[j:i] in wordDict:
dp[i] = True
break
return dp[-1]
# Example:
dictionary = {"leet", "code"}
print(word_break("leetcode", dictionary)) # Output: True
Place N queens on an N×N chessboard so that no two queens threaten each other.
def solve_n_queens(n):
def is_safe(row, col):
return not (cols[col] or hills[row - col] or dales[row + col])
def backtrack(row=0):
for col in range(n):
if is_safe(row, col):
queens.add((row, col))
cols[col] = 1
hills[row - col] = 1 # "hill" diagonals
dales[row + col] = 1 # "dale" diagonals
if row + 1 == n:
board = []
for r in range(n):
row_str = ""
for c in range(n):
row_str += "Q" if (r, c) in queens else "."
board.append(row_str)
solutions.append(board)
else:
backtrack(row + 1)
queens.remove((row, col))
cols[col] = 0
hills[row - col] = 0
dales[row + col] = 0
cols = [0] * n
hills = [0] * (2 * n - 1)
dales = [0] * (2 * n - 1)
queens = set()
solutions = []
backtrack()
return solutions
# Example:
print(solve_n_queens(4))
# Output: Two solutions printed as boards.
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
def trap(height):
if not height:
return 0
left, right = 0, len(height)-1
left_max, right_max = 0, 0
water = 0
while left < right:
if height[left] < height[right]:
if height[left] >= left_max:
left_max = height[left]
else:
water += left_max - height[left]
left += 1
else:
if height[right] >= right_max:
right_max = height[right]
else:
water += right_max - height[right]
right -= 1
return water
# Example:
print(trap([0,1,0,2,1,0,1,3,2,1,2,1])) # Output: 6
Design a data structure that supports adding numbers and finding the median efficiently.
import heapq
class MedianFinder:
def __init__(self):
self.small = [] # max heap (store negative values)
self.large = [] # min heap
def addNum(self, num: int) -> None:
heapq.heappush(self.small, -num)
if self.small and self.large and (-self.small[0]) > self.large[0]:
val = -heapq.heappop(self.small)
heapq.heappush(self.large, val)
if len(self.small) > len(self.large) + 1:
val = -heapq.heappop(self.small)
heapq.heappush(self.large, val)
if len(self.large) > len(self.small):
val = heapq.heappop(self.large)
heapq.heappush(self.small, -val)
def findMedian(self) -> float:
if len(self.small) > len(self.large):
return float(-self.small[0])
return (-self.small[0] + self.large[0]) / 2.0
# Example:
mf = MedianFinder()
mf.addNum(1)
mf.addNum(2)
print(mf.findMedian()) # Output: 1.5
mf.addNum(3)
print(mf.findMedian()) # Output: 2
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord such that only one letter can be changed at a time and each intermediate word must be in the dictionary.
from collections import deque
def word_ladder_length(beginWord, endWord, wordList):
wordSet = set(wordList)
if endWord not in wordSet:
return 0
queue = deque([(beginWord, 1)])
while queue:
word, length = queue.popleft()
if word == endWord:
return length
for i in range(len(word)):
for c in 'abcdefghijklmnopqrstuvwxyz':
next_word = word[:i] + c + word[i+1:]
if next_word in wordSet:
wordSet.remove(next_word)
queue.append((next_word, length+1))
return 0
# Example:
print(word_ladder_length("hit", "cog", ["hot","dot","dog","lot","log","cog"])) # Output: 5
Calculate minimum number of operations to convert one string to another with insert, delete, replace operations allowed.
def edit_distance(word1, word2):
m, n = len(word1), len(word2)
dp = [[0]*(n+1) for _ in range(m+1)]
for i in range(m+1):
dp[i][0] = i
for j in range(n+1):
dp[0][j] = j
for i in range(1, m+1):
for j in range(1, n+1):
if word1[i-1] == word2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = 1 + min(
dp[i-1][j], # delete
dp[i][j-1], # insert
dp[i-1][j-1] # replace
)
return dp[m][n]
# Example:
print(edit_distance("horse", "ros")) # Output: 3
If you want, I can provide detailed explanations or variations of these problems! Would you like me to generate hard questions for a specific topic?
Not yet included in a published release.