0% found this document useful (0 votes)
10 views16 pages

Coding

The document provides a collection of comprehensive Python coding questions commonly asked in IT company placements, including Experion Technologies. Each question includes a problem statement, sample input/output, and a Python solution. Topics covered include palindrome checking, Fibonacci series generation, prime number checking, array manipulation, sorting algorithms, and more.

Uploaded by

jayadev22ra125
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views16 pages

Coding

The document provides a collection of comprehensive Python coding questions commonly asked in IT company placements, including Experion Technologies. Each question includes a problem statement, sample input/output, and a Python solution. Topics covered include palindrome checking, Fibonacci series generation, prime number checking, array manipulation, sorting algorithms, and more.

Uploaded by

jayadev22ra125
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experion Technologies - Coding Assessment Questions

Comprehensive Python Coding Questions for Placement Preparation

Important Note: While these are the most commonly asked coding questions in IT company placements (including
companies like Experion Technologies), specific questions may vary. This collection covers all the fundamental
programming concepts typically tested in coding interviews.

Question 1: Check if a given string is a palindrome


Problem Statement: Write a function to check if a given string is a palindrome (reads the same forwards and
backwards).

Sample Input: "racecar"

Sample Output: True

Python Solution:

def is_palindrome(s):
s = [Link]().replace(" ", "")
return s == s[::-1]

# Test the function


test_string = "racecar"
result = is_palindrome(test_string)
print(f"Is '{test_string}' a palindrome? {result}")

Question 2: Generate Fibonacci series up to n terms

Problem Statement: Generate the Fibonacci series up to n terms where each number is the sum of the two preceding
ones.

Sample Input: n = 7

Sample Output: [0, 1, 1, 2, 3, 5, 8]

Python Solution:

def fibonacci_series(n):
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]

fib_list = [0, 1]
for i in range(2, n):
fib_list.append(fib_list[i-1] + fib_list[i-2])

return fib_list

# Test the function


n = 7
result = fibonacci_series(n)
print(f"Fibonacci series for {n} terms: {result}")

Question 3: Check if a number is prime

Problem Statement: Write a function to check if a given number is prime (divisible only by 1 and itself).

Sample Input: 17

Sample Output: True

Python Solution:

def is_prime(num):
if num < 2:
return False
if num == 2:
return True
if num % 2 == 0:
return False

for i in range(3, int(num**0.5) + 1, 2):


if num % i == 0:
return False
return True

# Test the function


test_num = 17
result = is_prime(test_num)
print(f"Is {test_num} a prime number? {result}")

Question 4: Reverse an array/list

Problem Statement: Write a function to reverse the elements of an array or list.

Sample Input: [1, 2, 3, 4, 5]

Sample Output: [5, 4, 3, 2, 1]

Python Solution:

def reverse_array(arr):
return arr[::-1]

# Alternative method using two pointers


def reverse_array_inplace(arr):
left, right = 0, len(arr) - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
return arr

# Test the function


test_array = [1, 2, 3, 4, 5]
result = reverse_array(test_array)
print(f"Reversed array: {result}")
Question 5: Calculate factorial of a number
Problem Statement: Write a function to calculate the factorial of a given number (n! = n × (n-1) × ... × 1).

Sample Input: 5

Sample Output: 120

Python Solution:

def factorial(n):
if n < 0:
return None
if n == 0 or n == 1:
return 1

result = 1
for i in range(2, n + 1):
result *= i
return result

# Recursive approach
def factorial_recursive(n):
if n < 0:
return None
if n == 0 or n == 1:
return 1
return n * factorial_recursive(n - 1)

# Test the function


test_num = 5
result = factorial(test_num)
print(f"Factorial of {test_num} is: {result}")

Question 6: Count number of vowels in a string


Problem Statement: Write a function to count the number of vowels (a, e, i, o, u) in a given string.

Sample Input: "hello world"

Sample Output: 3

Python Solution:

def count_vowels(s):
vowels = "aeiouAEIOU"
count = 0
for char in s:
if char in vowels:
count += 1
return count

# Alternative using list comprehension


def count_vowels_alt(s):
vowels = "aeiouAEIOU"
return sum(1 for char in s if char in vowels)

# Test the function


test_string = "hello world"
result = count_vowels(test_string)
print(f"Number of vowels in '{test_string}': {result}")
Question 7: Find maximum and minimum element in an array
Problem Statement: Write a function to find both the maximum and minimum elements in an array.

Sample Input: [3, 7, 1, 9, 4, 2, 8]

Sample Output: Maximum: 9, Minimum: 1

Python Solution:

def find_min_max(arr):
if not arr:
return None, None

minimum = maximum = arr[0]


for num in arr[1:]:
if num < minimum:
minimum = num
if num > maximum:
maximum = num

return minimum, maximum

# Alternative using built-in functions


def find_min_max_builtin(arr):
if not arr:
return None, None
return min(arr), max(arr)

# Test the function


test_array = [3, 7, 1, 9, 4, 2, 8]
min_val, max_val = find_min_max(test_array)
print(f"Array: {test_array}")
print(f"Minimum: {min_val}, Maximum: {max_val}")

Question 8: Find two numbers in array that add up to target sum


Problem Statement: Given an array and a target sum, find the indices of two numbers that add up to the target.

Sample Input: arr = [2, 7, 11, 15], target = 9

Sample Output: [0, 1] (indices of elements 2 and 7)

Python Solution:

def two_sum(nums, target):


num_dict = {}

for i, num in enumerate(nums):


complement = target - num
if complement in num_dict:
return [num_dict[complement], i]
num_dict[num] = i

return []

# Test the function


test_array = [2, 7, 11, 15]
target = 9
result = two_sum(test_array, target)
print(f"Array: {test_array}, Target: {target}")
print(f"Indices of two numbers that sum to target: {result}")

Question 9: Remove duplicates from an array

Problem Statement: Write a function to remove duplicate elements from an array.

Sample Input: [1, 2, 2, 3, 4, 4, 5]

Sample Output: [1, 2, 3, 4, 5]

Python Solution:

def remove_duplicates(arr):
return list(set(arr))

# Alternative maintaining order


def remove_duplicates_ordered(arr):
result = []
seen = set()
for item in arr:
if item not in seen:
[Link](item)
[Link](item)
return result

# Test the function


test_array = [1, 2, 2, 3, 4, 4, 5]
result = remove_duplicates_ordered(test_array)
print(f"Original array: {test_array}")
print(f"After removing duplicates: {result}")

Question 10: Implement binary search algorithm


Problem Statement: Implement binary search to find the index of a target element in a sorted array.

Sample Input: arr = [1, 3, 5, 7, 9, 11], target = 7

Sample Output: 3 (index of target element)

Python Solution:

def binary_search(arr, target):


left, right = 0, len(arr) - 1

while left <= right:


mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1

return -1

# Recursive approach
def binary_search_recursive(arr, target, left=0, right=None):
if right is None:
right = len(arr) - 1
if left > right:
return -1

mid = (left + right) // 2


if arr[mid] == target:
return mid
elif arr[mid] < target:
return binary_search_recursive(arr, target, mid + 1, right)
else:
return binary_search_recursive(arr, target, left, mid - 1)

# Test the function


test_array = [1, 3, 5, 7, 9, 11]
target = 7
result = binary_search(test_array, target)
print(f"Array: {test_array}, Target: {target}")
print(f"Index of target element: {result}")

Question 11: Implement bubble sort algorithm

Problem Statement: Implement the bubble sort algorithm to sort an array in ascending order.

Sample Input: [64, 34, 25, 12, 22, 11, 90]

Sample Output: [11, 12, 22, 25, 34, 64, 90]

Python Solution:

def bubble_sort(arr):
n = len(arr)
arr = [Link]() # Create a copy to avoid modifying original

for i in range(n):
swapped = False
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True

# If no swapping occurred, array is sorted


if not swapped:
break

return arr

# Test the function


test_array = [64, 34, 25, 12, 22, 11, 90]
result = bubble_sort(test_array)
print(f"Original array: {test_array}")
print(f"Sorted array: {result}")

Question 12: Count occurrences of each element in an array


Problem Statement: Write a function to count how many times each element appears in an array.

Sample Input: [1, 2, 3, 2, 1, 3, 1]

Sample Output: {1: 3, 2: 2, 3: 2}

Python Solution:
def count_occurrences(arr):
count_dict = {}
for item in arr:
count_dict[item] = count_dict.get(item, 0) + 1
return count_dict

# Alternative using Counter from collections


from collections import Counter

def count_occurrences_counter(arr):
return dict(Counter(arr))

# Test the function


test_array = [1, 2, 3, 2, 1, 3, 1]
result = count_occurrences(test_array)
print(f"Array: {test_array}")
print(f"Occurrences: {result}")

Question 13: Reverse a string without using built-in functions


Problem Statement: Write a function to reverse a string without using built-in reverse functions.

Sample Input: "hello"

Sample Output: "olleh"

Python Solution:

def reverse_string(s):
result = ""
for i in range(len(s) - 1, -1, -1):
result += s[i]
return result

# Alternative using two pointers


def reverse_string_two_pointers(s):
s_list = list(s)
left, right = 0, len(s_list) - 1

while left < right:


s_list[left], s_list[right] = s_list[right], s_list[left]
left += 1
right -= 1

return ''.join(s_list)

# Test the function


test_string = "hello"
result = reverse_string(test_string)
print(f"Original string: {test_string}")
print(f"Reversed string: {result}")

Question 14: Merge two sorted arrays into one sorted array
Problem Statement: Given two sorted arrays, merge them into a single sorted array.

Sample Input: arr1 = [1, 3, 5], arr2 = [2, 4, 6]

Sample Output: [1, 2, 3, 4, 5, 6]


Python Solution:

def merge_sorted_arrays(arr1, arr2):


merged = []
i = j = 0

while i < len(arr1) and j < len(arr2):


if arr1[i] <= arr2[j]:
[Link](arr1[i])
i += 1
else:
[Link](arr2[j])
j += 1

# Add remaining elements


while i < len(arr1):
[Link](arr1[i])
i += 1

while j < len(arr2):


[Link](arr2[j])
j += 1

return merged

# Test the function


arr1 = [1, 3, 5]
arr2 = [2, 4, 6]
result = merge_sorted_arrays(arr1, arr2)
print(f"Array 1: {arr1}")
print(f"Array 2: {arr2}")
print(f"Merged array: {result}")

Question 15: Check if two strings are anagrams


Problem Statement: Write a function to check if two strings are anagrams (contain the same characters in different
order).

Sample Input: "listen", "silent"

Sample Output: True

Python Solution:

def are_anagrams(s1, s2):


# Remove spaces and convert to lowercase
s1 = [Link](" ", "").lower()
s2 = [Link](" ", "").lower()

# If lengths are different, they can't be anagrams


if len(s1) != len(s2):
return False

# Sort both strings and compare


return sorted(s1) == sorted(s2)

# Alternative using character count


def are_anagrams_count(s1, s2):
s1 = [Link](" ", "").lower()
s2 = [Link](" ", "").lower()

if len(s1) != len(s2):
return False

char_count = {}

# Count characters in first string


for char in s1:
char_count[char] = char_count.get(char, 0) + 1

# Subtract character counts using second string


for char in s2:
if char not in char_count:
return False
char_count[char] -= 1
if char_count[char] == 0:
del char_count[char]

return len(char_count) == 0

# Test the function


str1, str2 = "listen", "silent"
result = are_anagrams(str1, str2)
print(f"'{str1}' and '{str2}' are anagrams: {result}")

Question 16: Find missing number in array of consecutive integers


Problem Statement: Find the missing number in an array containing consecutive integers with one number missing.

Sample Input: [1, 2, 4, 5, 6] (missing 3)

Sample Output: 3

Python Solution:

def find_missing_number(arr, n):


# Method 1: Using sum formula
expected_sum = n * (n + 1) // 2
actual_sum = sum(arr)
return expected_sum - actual_sum

# Method 2: Using XOR


def find_missing_number_xor(arr, n):
xor_all = 0
xor_arr = 0

# XOR all numbers from 1 to n


for i in range(1, n + 1):
xor_all ^= i

# XOR all numbers in array


for num in arr:
xor_arr ^= num

return xor_all ^ xor_arr

# Test the function


test_array = [1, 2, 4, 5, 6] # Missing 3, n = 6
n = 6
result = find_missing_number(test_array, n)
print(f"Array: {test_array}")
print(f"Missing number: {result}")
Question 17: Check if parentheses are balanced in a string
Problem Statement: Write a function to check if parentheses, brackets, and braces are properly balanced in a string.

Sample Input: "()[]{}" or "((()))"

Sample Output: True

Python Solution:

def is_valid_parentheses(s):
stack = []
mapping = {')': '(', '}': '{', ']': '['}

for char in s:
if char in mapping:
# It's a closing bracket
if not stack or [Link]() != mapping[char]:
return False
else:
# It's an opening bracket
[Link](char)

return len(stack) == 0

# Test the function


test_cases = ["()", "()[]{}", "((()))", "([)]", "{[}]"]
for test in test_cases:
result = is_valid_parentheses(test)
print(f"'{test}' has valid parentheses: {result}")

Question 18: Find Greatest Common Divisor (GCD) of two numbers


Problem Statement: Write a function to find the GCD (greatest common divisor) of two numbers.

Sample Input: 48, 18

Sample Output: 6

Python Solution:

def gcd(a, b):


# Using Euclidean algorithm
while b:
a, b = b, a % b
return a

# Recursive approach
def gcd_recursive(a, b):
if b == 0:
return a
return gcd_recursive(b, a % b)

# Using math library


import math

def gcd_builtin(a, b):


return [Link](a, b)

# Test the function


num1, num2 = 48, 18
result = gcd(num1, num2)
print(f"GCD of {num1} and {num2} is: {result}")

Question 19: Rotate a 2D matrix 90 degrees clockwise

Problem Statement: Rotate a 2D matrix 90 degrees clockwise.

Sample Input: [[1,2,3],[4,5,6],[7,8,9]]

Sample Output: [[7,4,1],[8,5,2],[9,6,3]]

Python Solution:

def rotate_matrix_90(matrix):
n = len(matrix)
# Create a new matrix for result
rotated = [[0] * n for _ in range(n)]

for i in range(n):
for j in range(n):
rotated[j][n-1-i] = matrix[i][j]

return rotated

# In-place rotation
def rotate_matrix_inplace(matrix):
n = len(matrix)

# Transpose the matrix


for i in range(n):
for j in range(i, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

# Reverse each row


for i in range(n):
matrix[i].reverse()

return matrix

# Test the function


test_matrix = [[1,2,3],[4,5,6],[7,8,9]]
result = rotate_matrix_90(test_matrix)
print("Original matrix:")
for row in test_matrix:
print(row)
print("\nRotated matrix:")
for row in result:
print(row)

Question 20: Find length of longest substring without repeating characters


Problem Statement: Find the length of the longest substring without repeating characters.

Sample Input: "abcabcbb"

Sample Output: 3 (abc)

Python Solution:
def longest_unique_substring(s):
char_index = {}
max_length = 0
start = 0

for end, char in enumerate(s):


if char in char_index and char_index[char] >= start:
start = char_index[char] + 1

char_index[char] = end
max_length = max(max_length, end - start + 1)

return max_length

# Alternative approach
def longest_unique_substring_alt(s):
if not s:
return 0

seen = set()
left = 0
max_length = 0

for right in range(len(s)):


while s[right] in seen:
[Link](s[left])
left += 1

[Link](s[right])
max_length = max(max_length, right - left + 1)

return max_length

# Test the function


test_string = "abcabcbb"
result = longest_unique_substring(test_string)
print(f"String: '{test_string}'")
print(f"Length of longest unique substring: {result}")

Question 21: Generate Pascal's Triangle with n rows


Problem Statement: Generate Pascal's Triangle with n rows where each number is the sum of the two numbers above
it.

Sample Input: 5

Sample Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Python Solution:

def generate_pascals_triangle(n):
triangle = []

for i in range(n):
row = [1] * (i + 1)

for j in range(1, i):


row[j] = triangle[i-1][j-1] + triangle[i-1][j]

[Link](row)

return triangle
# Function to print Pascal's triangle nicely
def print_pascals_triangle(triangle):
n = len(triangle)
for i, row in enumerate(triangle):
spaces = " " * (n - i - 1)
print(spaces + " ".join(map(str, row)))

# Test the function


n = 5
triangle = generate_pascals_triangle(n)
print(f"Pascal's Triangle with {n} rows:")
print_pascals_triangle(triangle)

Question 22: Calculate power of a number efficiently (x^n)


Problem Statement: Calculate x raised to the power n efficiently using binary exponentiation.

Sample Input: x = 2, n = 10

Sample Output: 1024

Python Solution:

def power(x, n):


# Handle negative exponents
if n < 0:
x = 1 / x
n = -n

result = 1
current_power = x

while n > 0:
if n % 2 == 1: # If n is odd
result *= current_power
current_power *= current_power
n //= 2

return result

# Recursive approach
def power_recursive(x, n):
if n == 0:
return 1
if n < 0:
return 1 / power_recursive(x, -n)

if n % 2 == 0:
half = power_recursive(x, n // 2)
return half * half
else:
return x * power_recursive(x, n - 1)

# Test the function


x, n = 2, 10
result = power(x, n)
print(f"{x}^{n} = {result}")
Question 23: Find single number in array where others appear twice
Problem Statement: In an array where every element appears twice except one, find the element that appears only
once.

Sample Input: [4,1,2,1,2] (4 appears once)

Sample Output: 4

Python Solution:

def find_single_number(nums):
# Using XOR - numbers appearing twice will cancel out
result = 0
for num in nums:
result ^= num
return result

# Alternative using dictionary


def find_single_number_dict(nums):
count = {}
for num in nums:
count[num] = [Link](num, 0) + 1

for num, freq in [Link]():


if freq == 1:
return num
return None

# Test the function


test_array = [4, 1, 2, 1, 2]
result = find_single_number(test_array)
print(f"Array: {test_array}")
print(f"Number appearing once: {result}")

Question 24: Check if string follows a given pattern

Problem Statement: Check if a string of words follows a given pattern where each character maps to a unique word.

Sample Input: pattern = 'abba', str = 'dog cat cat dog'

Sample Output: True

Python Solution:

def word_pattern(pattern, s):


words = [Link]()

if len(pattern) != len(words):
return False

char_to_word = {}
word_to_char = {}

for i in range(len(pattern)):
char = pattern[i]
word = words[i]

if char in char_to_word:
if char_to_word[char] != word:
return False
else:
char_to_word[char] = word

if word in word_to_char:
if word_to_char[word] != char:
return False
else:
word_to_char[word] = char

return True

# Test the function


pattern = "abba"
string = "dog cat cat dog"
result = word_pattern(pattern, string)
print(f"Pattern: '{pattern}'")
print(f"String: '{string}'")
print(f"Follows pattern: {result}")

Question 25: Count ways to climb n stairs

Problem Statement: Count the number of ways to climb n stairs if you can climb either 1 or 2 steps at a time.

Sample Input: n = 5

Sample Output: 8

Python Solution:

def climb_stairs(n):
if n <= 2:
return n

# Dynamic programming approach


dp = [0] * (n + 1)
dp[1] = 1
dp[2] = 2

for i in range(3, n + 1):


dp[i] = dp[i-1] + dp[i-2]

return dp[n]

# Space optimized version


def climb_stairs_optimized(n):
if n <= 2:
return n

prev2 = 1 # ways to climb 1 stair


prev1 = 2 # ways to climb 2 stairs

for i in range(3, n + 1):


current = prev1 + prev2
prev2 = prev1
prev1 = current

return prev1

# Test the function


n = 5
result = climb_stairs(n)
print(f"Number of ways to climb {n} stairs: {result}")
Additional Tips for Coding Interview Success

1. Time and Space Complexity

Always analyze the time and space complexity of your solutions


Try to optimize your code for better performance
Understand Big O notation

2. Problem-Solving Approach
Read the problem carefully
Ask clarifying questions
Think of edge cases
Start with a brute force solution, then optimize
Test your solution with examples

3. Common Data Structures to Know


Arrays/Lists

Strings
Hash Tables/Dictionaries

Stacks and Queues


Trees and Graphs

Linked Lists

4. Common Algorithms to Master


Searching: Linear Search, Binary Search

Sorting: Bubble Sort, Merge Sort, Quick Sort


Two Pointers Technique

Sliding Window
Dynamic Programming basics
Recursion

5. Python-Specific Tips

Use list comprehensions when appropriate


Leverage built-in functions (min, max, sum, sorted)
Know string methods (split, join, replace)

Understand slicing syntax


Use collections module (Counter, defaultdict)

Good luck with your Experion Technologies placement drive!

Remember to practice these problems multiple times and understand the logic behind each solution. Focus on writing
clean, readable code and always test your solutions with different inputs.

You might also like