0% found this document useful (0 votes)
3 views2 pages

DeltaX Coding Python Sol

The document outlines expected coding questions for DELTAX, including problems such as finding a pair with a given sum, checking if a string is a palindrome, counting set bits in an integer, and finding a missing number in a sequence. It provides example inputs and outputs along with Python function implementations for each problem. Additional tasks include finding the longest word in a sentence, calculating Fibonacci numbers with memoization, and printing elements of a matrix in spiral order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

DeltaX Coding Python Sol

The document outlines expected coding questions for DELTAX, including problems such as finding a pair with a given sum, checking if a string is a palindrome, counting set bits in an integer, and finding a missing number in a sequence. It provides example inputs and outputs along with Python function implementations for each problem. Additional tasks include finding the longest word in a sentence, calculating Fibonacci numbers with memoization, and printing elements of a matrix in spiral order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Expected Coding Questions for DELTAX

1)Find Pair with Given Sum


Given a list and a target, find a pair whose sum is equal to target.

Example:
Input: [2, 4, 7, 11, 15], target=9 → Output: (2, 7)

def find_pair(nums, target):


seen = set()
for num in nums:
if target - num in seen:
return (target - num, num)
seen.add(num)
return None

# Example
print(find_pair([2, 4, 7, 11, 15], 9))
Output: (2, 7)

2)Check if a String is Palindrome


Ignore case & spaces.

def is_palindrome(s):
s = ''.join(c.lower() for c in s if c.isalnum())
return s == s[::-1]

# Example
print(is_palindrome("A man a plan a canal Panama")) # True

3️
)Count Number of Set Bits
Given an integer, count how many bits are set to 1.

def count_set_bits(n):
count = 0
while n:
count += n & 1
n >>= 1
return count

print(count_set_bits(9)) # Output: 2

4) Find Missing Number


Numbers from 1 to N, one number missing.

Input: [1,2,4,5] → Output: 3

def find_missing_number(nums):
n = len(nums) + 1
expected_sum = n * (n+1) // 2
return expected_sum - sum(nums)

print(find_missing_number([1,2,4,5])) # Output: 3
5) Longest Word in Sentence
Return the longest word (if tie, return first).
def longest_word(sentence):
words = sentence.split()
return max(words, key=len)

print(longest_word("Find the longest word here"))


Output: "longest"

6) Fibonacci Number (with memoization)


Find nth Fibonacci number.

def fib(n, memo={}):


if n in memo: return memo[n]
if n <= 1: return n
memo[n] = fib(n-1, memo) + fib(n-2, memo)
return memo[n]

print(fib(10)) # Output: 55
7) Print Elements in Spiral Order (Matrix)
Input: 2D matrix; Output: list in spiral order.

def spiral_order(matrix):
res = []
while matrix:
res += matrix.pop(0)
if matrix and matrix[0]:
for row in matrix: res.append(row.pop())
if matrix:
res += matrix.pop()[::-1]
if matrix and matrix[0]:
for row in matrix[::-1]: res.append(row.pop(0))
return res

matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
]
print(spiral_order(matrix)) # [1,2,3,6,9,8,7,4,5]

You might also like