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

Programming Problems for Beginners

The document outlines a series of programming problems categorized into Beginner, Intermediate, and Advanced levels, each with a specific task and sample input/output. Each problem is worth 4 marks and covers various programming concepts such as arithmetic operations, string manipulation, data structures, and algorithms. The problems are designed to test the coding skills of individuals at different proficiency levels.

Uploaded by

mhproduction33
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 views26 pages

Programming Problems for Beginners

The document outlines a series of programming problems categorized into Beginner, Intermediate, and Advanced levels, each with a specific task and sample input/output. Each problem is worth 4 marks and covers various programming concepts such as arithmetic operations, string manipulation, data structures, and algorithms. The problems are designed to test the coding skills of individuals at different proficiency levels.

Uploaded by

mhproduction33
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

Beginner Problem

Q1. Sum of Two Numbers [4 Marks]

Problem: Write a program that takes two numbers as input and prints their sum.

●​ Sample Input: 10 20
●​ Sample Output: 30

Solution:

Q2. Area of a Circle [4 Marks]

Problem: Calculate the area of a circle given its radius. (Use pi = 3.1416)

●​ Sample Input: 5
●​ Sample Output: 78.54

Solution:

Q3. Even or Odd [4 Marks]

Problem: Check if a given integer is even or odd.

●​ Sample Input: 7
●​ Sample Output: Odd

Solution:

Q4. Maximum of Three Numbers [4 Marks]


Problem: Find the largest number among three input integers.

●​ Sample Input: 12 45 7
●​ Sample Output: 45

Solution:

Q5. Factorial Calculator [4 Marks]

Problem: Calculate the factorial of a given non-negative integer.

●​ Sample Input: 5
●​ Sample Output: 120

Solution:

Q6. Reverse a String [4 Marks]

Problem: Take a string input and print it in reverse order.

●​ Sample Input: Python


●​ Sample Output: nohtyP

Solution:

Q7. Palindrome Check [4 Marks]

Problem: Check if a given string is a palindrome (reads the same forward and backward).

●​ Sample Input: madam


●​ Sample Output: True
Solution:

Q8. Vowel Counter [4 Marks]

Problem: Count the number of vowels (a, e, i, o, u) in a given string.

●​ Sample Input: Education


●​ Sample Output: 5

Solution:

Q9. Fibonacci Sequence [4 Marks]

Problem: Print the first N numbers of the Fibonacci sequence.

●​ Sample Input: 6
●​ Sample Output: 0 1 1 2 3 5

Solution:

Q10. Check Prime Number [4 Marks]

Problem: Determine if a given positive integer is a prime number.

●​ Sample Input: 13
●​ Sample Output: Prime

Solution:
Q11. Sum of Digits [4 Marks]

Problem: Calculate the sum of all digits in a given integer.

●​ Sample Input: 1234


●​ Sample Output: 10

Solution:

Q12. Temperature Converter [4 Marks]

Problem: Convert a temperature from Celsius to Fahrenheit. Formula: (C * 9/5) + 32

●​ Sample Input: 25
●​ Sample Output: 77.0

Solution:

Q13. Leap Year Checker [4 Marks]

Problem: Check if a given year is a leap year.

●​ Sample Input: 2024


●​ Sample Output: Leap Year

Solution:
Q14. List Average [4 Marks]

Problem: Calculate the average of a list of numbers.

●​ Sample Input: [10, 20, 30, 40, 50]


●​ Sample Output: 30.0

Solution:

Q15. Remove Duplicates [4 Marks]

Problem: Remove duplicate elements from a list and print the unique elements.

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


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

Solution:

Q16. Second Largest Number [4 Marks]

Problem: Find the second largest number in a list of integers.

●​ Sample Input: [10, 20, 4, 45, 99]


●​ Sample Output: 45

Solution:

Q17. Character Frequency [4 Marks]

Problem: Count the frequency of each character in a string.


●​ Sample Input: hello
●​ Sample Output: {'h': 1, 'e': 1, 'l': 2, 'o': 1}

Solution:

Q18. Anagram Check [4 Marks]

Problem: Check if two strings are anagrams of each other (contain the same characters in different
orders).

●​ Sample Input: listen silent


●​ Sample Output: True

Solution:

Q19. Simple Interest [4 Marks]

Problem: Calculate simple interest given Principal, Rate, and Time. Formula: (P * R * T) / 100

●​ Sample Input: 1000 5 2 (P=1000, R=5, T=2)


●​ Sample Output: 100.0

Solution:

Q20. Multiplication Table [4 Marks]

Problem: Print the multiplication table for a given number up to 10.

●​ Sample Input: 3
●​ Sample Output: 3 6 9 12 15 18 21 24 27 30
Solution:

Q21. Count Words [4 Marks]

Problem: Count the number of words in a given sentence.

●​ Sample Input: Python is awesome


●​ Sample Output: 3

Solution:

Q22. GCD of Two Numbers [4 Marks]

Problem: Find the Greatest Common Divisor (GCD) of two integers.

●​ Sample Input: 48 18
●​ Sample Output: 6

Solution:

Q23. Armstrong Number [4 Marks]

Problem: Check if a number is an Armstrong number (sum of its digits raised to the power of the
number of digits equals the number itself).

●​ Sample Input: 153


●​ Sample Output: True

Solution:
Q24. Merge Two Dictionaries [4 Marks]

Problem: Merge two dictionaries into one.

●​ Sample Input: {'a': 1} {'b': 2}


●​ Sample Output: {'a': 1, 'b': 2}

Solution:

Q25. List Intersection [4 Marks]

Problem: Find the common elements between two lists.

●​ Sample Input: [1, 2, 3, 4] [3, 4, 5, 6]


●​ Sample Output: [3, 4]

Solution:

End of Exam
Intermediate Problem

Q1. Binary Search [4 Marks]

Problem: Implement a binary search algorithm to find the index of a target number in a sorted list. If
the number is not found, return -1.

●​ Sample Input: List: [1, 3, 5, 7, 9], Target: 5


●​ Sample Output: 2

Solution:

Q2. Valid Parentheses [4 Marks]

Problem: Given a string containing just the characters (, ), {, }, [ and ], determine if the input string is
valid (brackets are closed in the correct order).

●​ Sample Input: "{[]}"


●​ Sample Output: True

Solution:

Q3. Matrix Transpose [4 Marks]

Problem: Write a program to compute the transpose of a 3x3 matrix (swap rows with columns).

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


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

Solution:
Q4. Run-Length Encoding [4 Marks]

Problem: Compress a string by replacing consecutive repeating characters with the character
followed by the count.

●​ Sample Input: aaabbcddd


●​ Sample Output: a3b2c1d3

Solution:

Q5. Dictionary Inversion [4 Marks]

Problem: Invert a dictionary so that keys become values and values become keys. Assume values are
unique.

●​ Sample Input: {'a': 1, 'b': 2, 'c': 3}


●​ Sample Output: {1: 'a', 2: 'b', 3: 'c'}

Solution:

Q6. Group Anagrams [4 Marks]

Problem: Given a list of strings, group the anagrams together.

●​ Sample Input: ["eat", "tea", "tan", "ate", "nat", "bat"]


●​ Sample Output: [['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]

Solution:

Q7. Custom Exception Handling [4 Marks]


Problem: Write a function that takes an integer input. If the number is negative, raise a ValueError
with the message "Negative not allowed". Otherwise, print the square root.

●​ Sample Input: -4
●​ Sample Output: Error: Negative not allowed

Solution:

Q8. Flatten Nested List [4 Marks]

Problem: Write a recursive function to flatten a nested list of arbitrary depth.

●​ Sample Input: [1, [2, [3, 4], 5], 6]


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

Solution:

Q9. OOP: Bank Account Class [4 Marks]

Problem: Create a class BankAccount with deposit and withdraw methods. Initialize with zero
balance. Prevent withdrawal if funds are insufficient.

●​ Sample Input: Deposit 100, Withdraw 30, Withdraw 80


●​ Sample Output: Balance: 70, Insufficient Funds

Solution:

Q10. Lambda Sort with Tuples [4 Marks]

Problem: Sort a list of tuples based on the second element using a lambda function.
●​ Sample Input: [(1, 5), (3, 2), (2, 8)]
●​ Sample Output: [(3, 2), (1, 5), (2, 8)]

Solution:

Q11. Longest Common Prefix [4 Marks]

Problem: Find the longest common prefix string amongst an array of strings.

●​ Sample Input: ["flower", "flow", "flight"]


●​ Sample Output: "fl"

Solution:

Q12. Prime Factors [4 Marks]

Problem: Find all prime factors of a given number.

●​ Sample Input: 315


●​ Sample Output: [3, 3, 5, 7]

Solution:

Q13. Generator for Squares [4 Marks]

Problem: Write a generator function that yields the square of numbers from 0 to N.

●​ Sample Input: N=4


●​ Sample Output: 0 1 4 9 16

Solution:
Q14. Caesar Cipher [4 Marks]

Problem: Implement a Caesar Cipher that shifts each letter in a string by a fixed number (e.g., shift 3:
A->D).

●​ Sample Input: Text: "abc", Shift: 2


●​ Sample Output: "cde"

Solution:

Q15. Regex Email Validation [4 Marks]

Problem: Use Regular Expressions to validate if a string is a proper email format (simple check:
chars@[Link]).

●​ Sample Input: user@[Link]


●​ Sample Output: Valid

Solution:
Q16. Recursive Power Function [4 Marks]

Problem: Write a recursive function to calculate x raised to the power n.

●​ Sample Input: 2, 3
●​ Sample Output: 8

Solution:

Q17. Filter Palindromes [4 Marks]

Problem: Use filter() to extract all palindrome strings from a list.

●​ Sample Input: ["madam", "hello", "racecar", "world"]


●​ Sample Output: ['madam', 'racecar']

Solution:

Q18. Pascal’s Triangle [4 Marks]

Problem: Generate the first N rows of Pascal’s Triangle.

●​ Sample Input: 3
●​ Sample Output: [[1], [1, 1], [1, 2, 1]]

Solution:

Q19. Find Missing Number [4 Marks]

Problem: Given a list containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is
missing.
●​ Sample Input: [3, 0, 1]
●​ Sample Output: 2

Solution:

Q20. Variable Arguments (Args) [4 Marks]

Problem: Write a function using *args that accepts any number of integers and returns their product.

●​ Sample Input: multiply(2, 3, 4)


●​ Sample Output: 24

Solution:

Q21. Roman to Integer [4 Marks]

Problem: Convert a Roman numeral string (e.g., "IV", "X", "XIII") to an integer.

●​ Sample Input: "XIV"


●​ Sample Output: 14

Solution:

Q22. Decorator for Uppercase [4 Marks]

Problem: Write a decorator named make_upper that converts the return value of a function to
uppercase.

●​ Sample Input: Function returns "hello"


●​ Sample Output: "HELLO"
Solution:

Q23. Set Symmetric Difference [4 Marks]

Problem: Find the symmetric difference between two sets (elements present in either of the sets, but
not in both).

●​ Sample Input: A={1, 2, 3}, B={3, 4, 5}


●​ Sample Output: {1, 2, 4, 5}

Solution:

Q24. Parse CSV String [4 Marks]

Problem: Write a function that parses a CSV string (header + 1 row) into a dictionary.

●​ Sample Input: "name,age,city\nAlice,30,New York"


●​ Sample Output: {'name': 'Alice', 'age': '30', 'city': 'New York'}

Solution:

Q25. Map with Multiple Lists [4 Marks]

Problem: Use map() to add elements of two lists element-wise.

●​ Sample Input: [1, 2, 3], [4, 5, 6]


●​ Sample Output: [5, 7, 9]

Solution:
End of Exam
Advanced Problem

Q1. Reverse Linked List [4 Marks]

Problem: Given the head of a singly linked list, reverse the list and return the new head. Define the
ListNode class within your solution.

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


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

Solution:

Q2. Binary Tree Max Depth [4 Marks]

Problem: Write a function to find the maximum depth (height) of a binary tree.

●​ Sample Input: Root of tree: [3, 9, 20, null, null, 15, 7]


●​ Sample Output: 3

Solution:

Q3. LRU Cache Implementation [4 Marks]

Problem: Design a Least Recently Used (LRU) cache class with get and put methods. It should
support a fixed capacity and evict the least recently used item when full.

●​ Sample Input: Capacity: 2, put(1,1), put(2,2), get(1), put(3,3)


●​ Sample Output: get(1) returns 1; put(3) evicts key 2

Solution:
Q4. Merge Sort Algorithm [4 Marks]

Problem: Implement the Merge Sort algorithm to sort an array in ascending order (divide and
conquer approach).

●​ Sample Input: [12, 11, 13, 5, 6, 7]


●​ Sample Output: [5, 6, 7, 11, 12, 13]

Solution:

Q5. Graph BFS Traversal [4 Marks]

Problem: Implement Breadth-First Search (BFS) for a graph given as an adjacency list. Start from a
given source node.

●​ Sample Input: Graph: {0: [1, 2], 1: [2], 2: [0, 3], 3: [3]}, Start: 2
●​ Sample Output: 2 0 3 1

Solution:

Q6. Decorator with Arguments [4 Marks]

Problem: Write a decorator repeat(n) that executes the decorated function n times.

●​ Sample Input: @repeat(3) def say_hello(): print("Hi")


●​ Sample Output: "Hi" "Hi" "Hi"

Solution:

Q7. Detect Cycle in Linked List [4 Marks]


Problem: Use Floyd’s Cycle-Finding Algorithm (Tortoise and Hare) to determine if a linked list has a
cycle.

●​ Sample Input: 3 -> 2 -> 0 -> -4 (points back to 2)


●​ Sample Output: True

Solution:

Q8. 0/1 Knapsack Problem (DP) [4 Marks]

Problem: Given weights and values of n items, put these items in a knapsack of capacity W to get the
maximum total value. Use Dynamic Programming.

●​ Sample Input: Values: [60, 100, 120], Weights: [10, 20, 30], Capacity: 50
●​ Sample Output: 220

Solution:

Q9. Custom Context Manager [4 Marks]

Problem: Create a class-based Context Manager (using __enter__ and __exit__) that measures and
prints the execution time of a code block.

●​ Sample Input: with Timer(): [Link](1)


●​ Sample Output: Execution time: 1.00s

Solution:

Q10. Valid Binary Search Tree [4 Marks]

Problem: Write a function to validate if a binary tree is a valid Binary Search Tree (BST).
●​ Sample Input: [2, 1, 3] (Root 2, Left 1, Right 3)
●​ Sample Output: True

Solution:

Q11. Threading with Locks [4 Marks]

Problem: Simulate a bank account where two threads try to withdraw money simultaneously. Use a
Lock to prevent a race condition.

●​ Sample Input: Balance: 100, Thread A withdraws 80, Thread B withdraws 80


●​ Sample Output: Thread A: Success, Thread B: Insufficient funds

Solution:

Q12. Longest Increasing Subsequence [4 Marks]

Problem: Find the length of the longest strictly increasing subsequence in an unsorted array.

●​ Sample Input: [10, 9, 2, 5, 3, 7, 101, 18]


●​ Sample Output: 4 (Subsequence: [2, 3, 7, 18])

Solution:

Q13. Singleton Pattern [4 Marks]

Problem: Implement the Singleton design pattern so that a class allows only one instance to be
created.

●​ Sample Input: a = Singleton(); b = Singleton();


●​ Sample Output: a is b -> True
Solution:

Q14. Operator Overloading [4 Marks]

Problem: Create a Vector class representing a 2D vector. Overload the + operator (__add__) to
support vector addition.

●​ Sample Input: v1 = Vector(2, 4), v2 = Vector(1, -1); print(v1 + v2)


●​ Sample Output: Vector(3, 3)

Solution:

Q15. N-Queens Problem (Backtracking) [4 Marks]

Problem: Solve the N-Queens puzzle: place N queens on an NxN chessboard such that no two queens
attack each other. Return one valid configuration.

●​ Sample Input: N = 4
●​ Sample Output: [[0, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 0]] (positions)

Solution:

Q16. AsyncIO Basic [4 Marks]

Problem: Write an asynchronous function using async/await that waits for 1 second and then prints
"Done". Run it using the asyncio event loop.

●​ Sample Input: Run async function


●​ Sample Output: (Wait 1s)... Done

Solution:
Q17. Tree Implementation (Prefix Tree) [4 Marks]

Problem: Implement a Trie data structure with insert and startsWith (prefix search) methods.

●​ Sample Input: insert("apple"); startsWith("app")


●​ Sample Output: True

Solution:

Q18. Deep Copy vs Shallow Copy [4 Marks]

Problem: Demonstrate the difference between [Link]() and [Link]() using a list
containing nested lists.

●​ Sample Input: Modify nested element in shallow copy


●​ Sample Output: Original list changes (Shallow) vs Original remains same (Deep)

Solution:

Q19. Dijkstra’s Algorithm [4 Marks]

Problem: Find the shortest path from a source node to all other nodes in a weighted graph using
Dijkstra's algorithm.

●​ Sample Input: Graph: {A: {B: 1, C: 4}, B: {C: 2, D: 5}, C: {D: 1}}, Source: A
●​ Sample Output: {'A': 0, 'B': 1, 'C': 3, 'D': 4}

Solution:
Q20. Custom Iterator [4 Marks]

Problem: Create a class PowTwo that implements the iterator protocol (__iter__ and __next__) to
return powers of two up to a maximum exponent.

●​ Sample Input: max = 3


●​ Sample Output: 1 2 4 8

Solution:

Q21. Abstract Base Classes [4 Marks]

Problem: Define an Abstract Base Class Shape with an abstract method area. Create a subclass
Rectangle that implements it.

●​ Sample Input: Rectangle(3, 4).area()


●​ Sample Output: 12

Solution:

Q22. Topological Sort [4 Marks]

Problem: Given a Directed Acyclic Graph (DAG), perform a topological sort (useful for task
scheduling).

●​ Sample Input: Edges: [(5, 2), (5, 0), (4, 0), (4, 1), (2, 3), (3, 1)]
●​ Sample Output: 5 4 2 3 1 0 (One possible order)

Solution:
Q23. Generator Pipelines [4 Marks]

Problem: Chain two generators: one that yields numbers 1 to 10, and a second that squares the
numbers yielded by the first.

●​ Sample Input: Iterate pipeline


●​ Sample Output: 1 4 9 ... 100

Solution:

Q24. Edit Distance (Levenshtein) [4 Marks]

Problem: Calculate the minimum number of operations (insert, delete, replace) required to convert
word1 to word2.

●​ Sample Input: "horse", "ros"


●​ Sample Output: 3

Solution:

Q25. Binary Search Tree Iterator [4 Marks]

Problem: Implement an iterator over a binary search tree (BST). The iterator is initialized with the
root node of a BST and uses a stack to iterate in ascending order (in-order traversal).

●​ Sample Input: next(), next() on BST [7, 3, 15]


●​ Sample Output: 3, 7

Solution:
End of Exam

You might also like