There is a moment every beginner knows well. You open a coding platform, stare at the screen, and feel completely paralyzed because nobody told you where to begin, what to practice, or how to know if you are making real progress.
This blog hands you 45 carefully selected best coding challenges for beginners with their solutions and organized by category. Whether you are learning Python, Java, C++, or JavaScript, the logic behind each problem is universal. The code is the vehicle; the thinking is the destination. These challenges are also designed to align with current software development trends, where problem-solving and logical thinking matter more than ever.
If you are still building your foundation, going through top programming tutorials before diving into challenges can make the learning curve much smoother.
45 Coding Challenges for Beginners with Solutions
These 45 coding challenges for beginners cover strings, arrays, sorting, recursion, and hashing; all with clean, working solutions to guide you through every step.
Category: String Problems (Challenges 1–8)
Strings are among the first data types that any programmer encounters, and the problems built around them cover a surprisingly wide range of logical thinking. String manipulation is one of the first things you work on when you Learn How to Code, making these challenges a natural starting point.
Challenge 1: Reverse a String
Problem: Write a function that takes a string as input and returns it reversed.
Solution:
def reverse_string(s):
return s[::-1]
print(reverse_string(“hello”)) # Output: “olleh”
Challenge 2: Check if a String is a Palindrome
Problem: Determine whether a given string reads the same forwards and backwards.
Solution :
def is_palindrome(s):
s = s.lower().replace(” “, “”)
return s == s[::-1]
print(is_palindrome(“racecar”)) # Output: True
print(is_palindrome(“hello”)) # Output: False
Challenge 3: Count Vowels in a String
Problem: Given a string, return the total count of vowels (a, e, i, o, u).
Solution:
def count_vowels(s):
count = 0
for char in s.lower():
if char in “aeiou”:
count += 1
return count
print(count_vowels(“Programming”)) # Output: 3
Challenge 4: Check if Two Strings are Anagrams
Problem: Two strings are anagrams if they contain exactly the same characters in any order. Return True or False.
Solution :
def are_anagrams(s1, s2):
return sorted(s1.lower()) == sorted(s2.lower())
print(are_anagrams(“listen”, “silent”)) # Output: True
print(are_anagrams(“hello”, “world”)) # Output: False
Challenge 5: Find the Frequency of Each Character
Problem: Given a string, return a dictionary showing how many times each character appears.
Solution:
def char_frequency(s):
freq = {}
for char in s:
freq[char] = freq.get(char, 0) + 1
return freq
print(char_frequency(“banana”))
# Output: {‘b’: 1, ‘a’: 3, ‘n’: 2}
Challenge 6: Find the Longest Word in a Sentence
Problem: Given a sentence, return the word that contains the most characters.
Solution:
def longest_word(sentence):
words = sentence.split()
longest = “”
for word in words:
if len(word) > len(longest):
longest = word
return longest
print(longest_word(“I love programming every day”))
# Output: “programming”
Challenge 7: Count the Number of Words in a String
Problem: Without using a built-in word-count method, determine how many words are in a given sentence.
Solution :
def count_words(sentence):
words = sentence.strip().split()
return len(words)
print(count_words(” Hello world “)) # Output: 2
Challenge 8: Count the Number of Vowels and Consonants Separately
Problem: Given a string, count how many vowels and how many consonants it contains and return both counts.
Solution:
def count_vowels_consonants(s):
vowels = 0
consonants = 0
for char in s.lower():
if char.isalpha():
if char in “aeiou”:
vowels += 1
else:
consonants += 1
return vowels, consonants
print(count_vowels_consonants(“Hello World”))
# Output: (3, 7)
Category: Array Problems (Challenges 9–18)
Arrays are the backbone of data structures. These problems build your ability to manipulate, search, and transform collections of data efficiently.
Challenge 9: Find the Maximum and Minimum Element
Problem: Given an unsorted array of integers, return both the largest and smallest values.
Solution :
def find_max_min(arr):
return max(arr), min(arr)
print(find_max_min([3, 1, 7, 2, 9, 4]))
# Output: (9, 1)
Challenge 10: Reverse an Array In Place
Problem: Reverse the elements of an array without creating a new one.
Solution :
def reverse_array(arr):
left, right = 0, len(arr) – 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
return arr
print(reverse_array([1, 2, 3, 4, 5]))
# Output: [5, 4, 3, 2, 1]
Challenge 11: Find the Second Largest Element
Problem: Return the second highest value in an array without sorting it.
Solution:
def second_largest(arr):
first = second = float(‘-inf’)
for num in arr:
if num > first:
second = first
first = num
elif num > second and num != first:
second = num
return second
print(second_largest([10, 5, 8, 20, 3]))
# Output: 10
Challenge 12: Remove Duplicates from a Sorted Array
Problem: Given a sorted array, remove all repeated elements and return the unique elements.
Solution :
def remove_duplicates(arr):
if not arr:
return []
result = [arr[0]]
for i in range(1, len(arr)):
if arr[i] != arr[i – 1]:
result.append(arr[i])
return result
print(remove_duplicates([1, 1, 2, 3, 3, 4, 5, 5]))
# Output: [1, 2, 3, 4, 5]
Challenge 13: Rotate an Array by K Positions
Problem: Shift all elements of an array to the right by K steps.
Solution :
def rotate_array(arr, k):
k = k % len(arr)
return arr[-k:] + arr[:-k]
print(rotate_array([1, 2, 3, 4, 5], 2))
# Output: [4, 5, 1, 2, 3]
Challenge 14: Find the Intersection of Two Arrays
Problem: Given two arrays, return a list of elements that appear in both arrays, without duplicates.
Solution:
def array_intersection(arr1, arr2):
set1 = set(arr1)
set2 = set(arr2)
return list(set1 & set2)
print(array_intersection([1, 2, 3, 4, 5], [3, 4, 5, 6, 7]))
# Output: [3, 4, 5]
print(array_intersection([1, 2, 2, 3], [2, 3, 3, 4]))
# Output: [2, 3]
Challenge 15: Find All Pairs That Sum to a Target
Problem: Return all pairs of elements in an array whose sum equals a given target value.
Solution :
def find_pairs(arr, target):
pairs = []
seen = set()
for num in arr:
complement = target – num
if complement in seen:
pairs.append((complement, num))
seen.add(num)
return pairs
print(find_pairs([1, 5, 3, 7, 2, 8], 10))
# Output: [(3, 7), (2, 8)]
Challenge 16: Move All Zeroes to the End
Problem: Rearrange elements so all zeroes appear at the end while maintaining the relative order of non-zero elements.
Solution :
def move_zeroes(arr):
position = 0
for num in arr:
if num != 0:
arr[position] = num
position += 1
while position < len(arr):
arr[position] = 0
position += 1
return arr
print(move_zeroes([0, 1, 0, 3, 12]))
# Output: [1, 3, 12, 0, 0]
Challenge 17: Merge Two Sorted Arrays
Problem: Combine two already-sorted arrays into a single sorted array.
Solution :
def merge_sorted(a, b):
result = []
i = j = 0
while i < len(a) and j < len(b):
if a[i] <= b[j]:
result.append(a[i])
i += 1
else:
result.append(b[j])
j += 1
result.extend(a[i:])
result.extend(b[j:])
return result
print(merge_sorted([1, 3, 5], [2, 4, 6]))
# Output: [1, 2, 3, 4, 5, 6]
Challenge 18: Find the Sum of All Elements
Problem: Given an array of integers, return the total sum of all elements.
Solution :
def array_sum(arr):
total = 0
for num in arr:
total += num
return total
print(array_sum([1, 2, 3, 4, 5])) # Output:
Category: Math and Number Problems (Challenges 19–25)
Number problems sharpen your logical reasoning and introduce several fundamental algorithms that appear repeatedly across computer science regardless of which programming language you choose to practice with
Challenge 19: Check if a Number is Prime
Problem: Determine whether a given number has exactly two divisors: 1 and itself.
Solution :
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
print(is_prime(17)) # Output: True
print(is_prime(18)) # Output: False
Challenge 20: Generate the Fibonacci Sequence
Problem: Print the Fibonacci series up to N terms, where each number is the sum of the two preceding ones.
Solution :
def fibonacci(n):
a, b = 0, 1
sequence = []
for _ in range(n):
sequence.append(a)
a, b = b, a + b
return sequence
print(fibonacci(8))
# Output: [0, 1, 1, 2, 3, 5, 8, 13]
Challenge 21: Find the Factorial of a Number
Problem: Calculate the factorial of a given non-negative integer.
Solution:
def factorial(n):
result = 1
for i in range(2, n + 1):
result *= i
return result
print(factorial(5)) # Output: 120
Challenge 22: Reverse the Digits of a Number
Problem: Without converting to a string, reverse the digits of an integer mathematically.
Solution :
def reverse_number(n):
reversed_num = 0
is_negative = n < 0
n = abs(n)
while n > 0:
digit = n % 10
reversed_num = reversed_num * 10 + digit
n //= 10
return -reversed_num if is_negative else reversed_num
print(reverse_number(12345)) # Output: 54321
print(reverse_number(-678)) # Output: -876
Challenge 23: Find the Sum of Digits
Problem: Given an integer, return the sum of all its individual digits.
Solution :
def sum_of_digits(n):
n = abs(n)
total = 0
while n > 0:
total += n % 10
n //= 10
return total
print(sum_of_digits(1234)) # Output: 10
Challenge 24: Find the LCM of Two Numbers
Problem: Given two positive integers, find their Least Common Multiple (LCM) without using any built-in library functions.
Solution :
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return (a * b) // gcd(a, b)
print(lcm(4, 6)) # Output: 12
print(lcm(12, 18)) # Output: 36
Challenge 25: Check if a Number is an Armstrong Number
Problem: An Armstrong number (also called a narcissistic number) is one where the sum of its digits, each raised to the power of the number of digits, equals the number itself. For example, 153 = 1³ + 5³ + 3³. Write a function to check if a given number is an Armstrong number.
Solution :
def is_armstrong(n):
digits = str(n)
power = len(digits)
total = sum(int(d) ** power for d in digits)
return total == n
print(is_armstrong(153)) # Output: True
print(is_armstrong(370)) # Output: True
print(is_armstrong(123)) # Output: False
Category: Recursion Problems (Challenges 26–30)
Recursion is where beginner programmers often hit their first major wall. Alongside practice, tuning into the best coding podcasts can help you hear how experienced developers think through problems like these.
Challenge 26: Calculate Power Using Recursion
Problem: Compute base raised to the power of exponent recursively.
Solution :
def power(base, exp):
if exp == 0:
return 1
return base * power(base, exp – 1)
print(power(2, 10)) # Output: 1024
Challenge 27: Sum an Array Using Recursion
Problem: Find the sum of all elements in an array using recursion, without any loops.
Solution:
def recursive_sum(arr):
if not arr:
return 0
return arr[0] + recursive_sum(arr[1:])
print(recursive_sum([1, 2, 3, 4, 5])) # Output: 15
Challenge 28: Reverse a String Using Recursion\
Problem: Reverse a string by recursively solving a smaller version of the same problem.
Solution:
def reverse_recursive(s):
if len(s) <= 1:
return s
return reverse_recursive(s[1:]) + s[0]
print(reverse_recursive(“hello”)) # Output: “olleh”
Challenge 29: Find the nth Fibonacci Number Using Recursion
Problem: Return the nth number in the Fibonacci sequence using recursion.
Solution :
def fib_recursive(n):
if n <= 1:
return n
return fib_recursive(n – 1) + fib_recursive(n – 2)
print(fib_recursive(7)) # Output: 13
Challenge 30: Count Occurrences of a Digit Using Recursion
Problem: Determine how many times a specific digit appears in a number without converting to a string.
Solution :
def count_digit(n, d):
if n == 0:
return 1 if d == 0 else 0
if n < 0:
n = -n
count = 0
while n > 0:
if n % 10 == d:
count += 1
n //= 10
return count
print(count_digit(122333, 3)) # Output: 3
Category: Hashing and Stacks (Challenges 31–34)
These problems introduce you to two of the most powerful tools in a programmer’s toolkit; hash maps for fast lookups and stacks for managing ordered operations. Hash maps mirror the fast-lookup logic that powers real-world databases, including how SQL server administration manages user permissions and access internally.
Challenge 31: Check for Balanced Parentheses
Problem: Use a stack to determine whether every opening bracket in a string has a correctly matched closing bracket.
Solution :
def is_balanced(s):
stack = []
mapping = {‘)’: ‘(‘, ‘}’: ‘{‘, ‘]’: ‘[‘}
for char in s:
if char in mapping.values():
stack.append(char)
elif char in mapping:
if not stack or stack[-1] != mapping[char]:
return False
stack.pop()
return len(stack) == 0
print(is_balanced(“({[]})”)) # Output: True
print(is_balanced(“({[})”)) # Output: False
Challenge 32: Implement a Queue Using Two Stacks
Problem: Simulate first-in, first-out (queue) behavior using only stack operations.
Solution :
class QueueUsingStacks:
def __init__(self)
self.stack1 = []
self.stack2 = []
def enqueue(self, item):
self.stack1.append(item)
def dequeue(self):
if not self.stack2:
while self.stack1:
self.stack2.append(self.stack1.pop())
return self.stack2.pop() if self.stack2 else None
q = QueueUsingStacks()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
print(q.dequeue()) # Output: 1
print(q.dequeue()) # Output: 2
Challenge 33: Find the First Non-Repeating Character
Problem: Traverse a string and return the first character that appears exactly once.
Solution :
def first_non_repeating(s):
freq = {}
for char in s:
freq[char] = freq.get(char, 0) + 1
for char in s:
if freq[char] == 1:
return char
return None
print(first_non_repeating(“aabbcde”)) # Output: “c”
print(first_non_repeating(“aabb”)) # Output: None
Challenge 34: Find Two Numbers That Add Up to a Target Using Hashing
Problem: Given an array and a target, return the indices of the two numbers that add up to the target.
Solution :
def two_sum(arr, target):
seen = {}
for i, num in enumerate(arr):
complement = target – num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return []
print(two_sum([2, 7, 11, 15], 9)) # Output: [0, 1]
print(two_sum([3, 2, 4], 6)) # Output: [1, 2]
If you’re working with real data at scale, understanding how to fix slow MySQL queries becomes the natural next step because the same hashing principles that make this solution fast are what database indexing relies on.
Category: Sorting, Ciphers, and Number Theory (35–40)
Once you are comfortable with sorting problems, practicing on dedicated Coding Kata Sites can sharpen your speed and consistency further.
Challenge 35: Bubble Sort an Array
Problem: Sort an array of integers in ascending order using the bubble sort algorithm.
Solution :
def bubble_sort(arr):
n = len(arr)
for i in range(n – 1):
for j in range(0, n – i – 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
print(bubble_sort([64, 34, 25, 12, 22, 11, 90]))
# Output: [11, 12, 22, 25, 34, 64, 90]
Challenge 36: Implement the Caesar Cipher
Problem: Encrypt a string by shifting each letter forward in the alphabet by a given number of positions. Non-letter characters remain unchanged.
Solution:
def caesar_cipher(text, shift):
result = “”
for char in text:
if char.isalpha():
base = ord(‘A’) if char.isupper() else ord(‘a’)
result += chr((ord(char) – base + shift) % 26 + base)
else:
result += char
return result
print(caesar_cipher(“Hello, World!”, 3))
# Output: “Khoor, Zruog!”
Challenge 37: Convert Celsius to Fahrenheit (and Back)
Problem: Write two functions; one that converts Celsius to Fahrenheit, and another that reverses the conversion.
Solution :
def celsius_to_fahrenheit(c):
return (c * 9/5) + 32
def fahrenheit_to_celsius(f):
return (f – 32) * 5/9
print(celsius_to_fahrenheit(100)) # Output: 212.0
print(fahrenheit_to_celsius(212)) # Output: 100.0
Challenge 38: Convert a Number from Decimal to Binary
Problem: Without using Python’s built-in bin() function, convert a positive decimal integer to its binary representation as a string.
Solution :
def decimal_to_binary(n):
if n == 0:
return “0”
binary = “”
while n > 0:
binary = str(n % 2) + binary
n //= 2
return binary
print(decimal_to_binary(13)) # Output: “1101”
print(decimal_to_binary(0)) # Output: “0”
Challenge 39: Find the GCD of Two Numbers
Problem: Calculate the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm.
Solution :
def gcd(a, b):
while b:
a, b = b, a % b
return a
print(gcd(48, 18)) # Output: 6
print(gcd(100, 75)) # Output: 25
Challenge 40: Insertion Sort
Problem: Sort an array of integers in ascending order using the insertion sort algorithm, without using any built-in sort functions.
Solution:
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i – 1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
print(insertion_sort([12, 11, 13, 5, 6]))
# Output: [5, 6, 11, 12, 13]
Category: Geometry, Sorting, and String Manipulation (41-45)
Challenge 41: Calculate the Distance Between Two Points
Problem: Given two points on a 2D plane, each defined by their x and y coordinates, calculate the straight-line distance between them.
Solution:
import math
def distance_between_points(x1, y1, x2, y2):
return math.sqrt((x2 – x1)**2 + (y2 – y1)**2)
print(distance_between_points(1, 2, 4, 6))
# Output: 5.0
Challenge 42: Selection Sort an Array
Problem: Sort an array of integers in ascending order using the selection sort algorithm without relying on any built-in sort functions.
Solution:
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
return arr
print(selection_sort([64, 25, 12, 22, 11]))
# Output: [11, 12, 22, 25, 64]
Challenge 43: Capitalize the First Letter of Every Word
Problem: Given a sentence, return the same sentence with the first letter of every word capitalized. Do this without using Python’s built-in .title() method.
Solution:
def capitalize_words(sentence):
words = sentence.split()
result = []
for word in words:
if word:
result.append(word[0].upper() + word[1:])
return ” “.join(result)
print(capitalize_words(“the quick brown fox”))
# Output: “The Quick Brown Fox”
Challenge 44: Filter Only Positive Numbers from an Array
Problem: Given an array that contains a mix of positive numbers, negative numbers, and zeroes, return a new array containing only the positive values.
Solution:
def filter_positives(arr):
result = []
for num in arr:
if num > 0:
result.append(num)
return result
print(filter_positives([-3, 0, 5, -1, 8, -7, 2]))
# Output: [5, 8, 2]
Challenge 45: Convert Binary String to Decimal Number
Problem: Given a binary number represented as a string (for example, “1101”), convert it to its decimal equivalent without using Python’s built-in int() with a base parameter.
Solution:
def binary_to_decimal(binary_str):
decimal = 0
power = 0
for digit in reversed(binary_str):
decimal += int(digit) * (2 ** power
power += 1
return decimal
print(binary_to_decimal(“1101”)) # Output: 13
print(binary_to_decimal(“10101”)) # Output: 21
Conclusion
Every challenge in this list exists for a reason. These 45 best coding challenges for beginners are not just exercises; they are the building blocks of real problem-solving skills. Strings, arrays, recursion, hashing; each category pushes your thinking one step further. Stay consistent, revisit the problems you found difficult, and trust the process. The progress will show up before you expect it.
Do not rush through these questions. Understand each solution, then try writing it again from memory. That gap between reading and doing is exactly where real learning happens.
If solving these problems has sparked an interest in writing about code, we welcome contributors. Check out our web development write for us page and share your expertise with our readers.
Share on media