1. Find Nth term in mixed Fibonacci + Prime series
Question:
The series is:
1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, ...
Odd positions form Fibonacci series and even positions form prime numbers. Find the Nth term.
Example:
Input: 14 Output: 17
Python:
def is_prime(n): if n < 2: return False for i in range(2, int(n**0.5)+1): if n % i == 0: return False return True n = int(input()) fib = [1, 1] while len(fib) < n: fib.append(fib[-1] + fib[-2]) primes = [] num = 2 while len(primes) < n: if is_prime(num): primes.append(num) num += 1 if n % 2 == 1: print(fib[n//2]) else: print(primes[n//2 - 1]). Generate Alternate Geometric Series
Question:
Given a series where odd positions and even positions create separate geometric progressions.Example:
Input: 5 Output: 1 1 2 2 4 4 8 8 16 16Python:
n = int(input()) a = 1 result = [] for i in range(n): result.append(a) result.append(a) a *= 2 print(*result)
3. Check Anagram of Two Strings
Question:
Check whether two strings are anagrams.Example:
Input: abaac aabca Output: TruePython:
s1 = input() s2 = input() if sorted(s1) == sorted(s2): print("True") else: print("False")
4. Remove Consecutive Duplicate Digits
Question:
Remove repeated consecutive digits.Example:
Input: 11223345 Output: 12345Python:
s = input() ans = s[0] for i in range(1, len(s)): if s[i] != s[i-1]: ans += s[i] print(ans)
5. Find Element Appearing Only Once
Question:
Given an array where every element appears twice except one element, find that element.Example:
Input: 5 1 2 3 2 1 Output: 3Python:
n = int(input()) arr = list(map(int,input().split())) x = 0 for i in arr: x ^= i print(x)
6. Matrix Trace
Question:
Find the trace of a square matrix.Example:
Input: 3 1 2 3 4 5 6 7 8 9 Output: 15Python:
n = int(input()) matrix = [] for _ in range(n): matrix.append(list(map(int,input().split()))) trace = 0 for i in range(n): trace += matrix[i][i] print(trace)
7. Merge Two Arrays Alternately
Question:
Merge two arrays by taking alternate elements. Add remaining elements from the larger array.Example:
A = [1,2,3] B = [4,5,6,7] Output: 1 4 2 5 3 6 7Python:
a = list(map(int,input().split())) b = list(map(int,input().split())) result = [] i = j = 0 while i < len(a) and j < len(b): result.append(a[i]) result.append(b[j]) i += 1 j += 1 result.extend(a[i:]) result.extend(b[j:]) print(*result)
8. Count Frequency of Characters
Question:
Find the frequency of each character in a string.Example:
Input: banana Output: b:1 a:3 n:2Python:
s = input() freq = {} for ch in s: freq[ch] = freq.get(ch,0)+1 for k,v in freq.items(): print(k,":",v)
9. Second Largest Number
Question:
Find the second largest element without sorting.arr = list(map(int,input().split())) largest = second = float('-inf') for x in arr: if x > largest: second = largest largest = x elif x > second and x != largest: second = x print(second)
10. Sum of Digits Until Single Digit
Example:
Input: 9875 9+8+7+5 = 29 2+9 = 11 1+1 = 2 Output: 2Python:
n = int(input()) while n >= 10: s = 0 while n: s += n % 10 n //= 10 n = s print(n)
Important topics to prepare for TCS NQT Python:
- Arrays and strings
- Hashing/frequency counting
- Number theory (prime, palindrome, factors)
- Matrix problems
- Sorting/searching
- Basic recursion
- Stack and queue implementation
- Pattern/series problems
No comments:
Post a Comment