Coding Problems in DSA: A Beginner’s Guide to Practice and Solutions (2026)
Sep 02, 2026 7 Min Read 4261 Views
(Last Updated)
Coding Problems in DSA are questions built around arrays, linked lists, recursion, and sorting that test whether you can actually apply what you’ve learned, not just recite it. Each one forces you to break a bigger challenge into smaller steps and figure out the most efficient way to solve it.
If you’re just starting out, don’t jump into the hardest problems you find online. Pick easy ones, solve them slowly, and understand why the solution works before moving to the next. Over time, this habit builds the kind of thinking that makes tougher problems feel far less intimidating.
Table of contents
- TL;DR Summary
- Key Concepts to Master for Solving DSA Problems
- List of Basic Coding Problems in DSA for Beginners
- Print all prime numbers within a given range (Loops and Conditionals)
- Find the sum of digits of a number (Loops and Conditionals)
- Find the second largest element in an array (Arrays and Strings)
- Reverse a string without using built-in functions (Arrays and Strings)
- Implement Linear Search (Searching)
- Implement Binary Search on a sorted array (Searching)
- Sort an array using Bubble Sort (Sorting)
- Sort an array using Insertion Sort (Sorting)
- Find the factorial of a number using recursion (Basic Math and Number Problem)
- Print the Fibonacci series using recursion (Recursion)
- Print a pyramid star pattern (Patterns)
- Print a number triangle pattern (Patterns)
- Insert a node at the end of a linked list (Linked List)
- Implement push and pop operations in a stack (Stack)
- Check if a number is a palindrome (Basic Math and Number Problems)
- Coding Problems in DSA: Quick Reference Table
- Conclusion
- FAQs
- How many Coding Problems in DSA should a beginner solve daily?
- Which topic should I start with first?
- Do I need a specific programming language to solve these?
- Are these problems enough for interview preparation?
- What's the best way to practice these problems?
TL;DR Summary
- This guide breaks down 15 essential Coding Problems in DSA, covering everything from loops and arrays to recursion and linked lists.
- Each problem is explained in plain terms, so you understand the logic before jumping into code.
- A quick reference table lists every problem along with its category, difficulty level, and the core concept it teaches.
- The Coding Problems in DSA are grouped by topic, making it easy to focus on one area at a time, like sorting, searching, or patterns.
- By the end, you’ll have a clear starting point for practicing Coding Problems in DSA and building real problem-solving skills.
Key Concepts to Master for Solving DSA Problems

Before indulging in solving DSA problems, you need a solid understanding of essential topics. Start by gaining a firm grasp of input and output programs and, simultaneously, practice questions on loops and conditional statements to develop a fundamental understanding of how the logic flows during program execution.
Then move on to array and string problems, which will help you handle and manage data efficiently. After that, you should explore basic search and sorting methods, such as linear search and bubble sort.
Once you’ve completed all this, try tackling simple recursion problems and pattern-based printing to strengthen your analytical thinking.
As you progress, start learning other data structures, such as stacks, queues, and linked lists, as well as math-based topics like numbers, factorials, and palindromes. These are key concepts to master for solving DSA problems.
Note: We use JavaScript as the primary language to explore the following fundamental DSA problems. You can also use other programming languages, such as C++, Java, or Python (the logic is the same across languages; only the syntax differs).
Strengthen your programming skills with our comprehensive and affordable HCL GUVI’s DSA course: DSA for Programmers Course
Ready to build real projects? The HCL GUVI’s Software and AI Engineer Course teaches DSA, full stack development, and AI-powered workflows through hands-on projects, expert mentorship, and mock interviews, everything you need to turn practice problems into a job-ready portfolio. Enroll now and start building.
List of Basic Coding Problems in DSA for Beginners
The following are the fundamental coding problems in DSA we have mentioned, along with the category to which each belongs:
1. Print all prime numbers within a given range (Loops and Conditionals)
Check every number in the range and print only the ones that can’t be divided evenly by anything except 1 and themselves.
function PrimesInRange(start, end) {
for (let num = start; num <= end; num++) {
let isPrime = true;
if (num < 2) continue; // 0 and 1 are not prime
for (let i = 2; i * i <= num; i++) {
if (num % i === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
console.log(num);
}
}
}
PrimesInRange(10, 30);
Output:
11
13
17
19
23
29
Explanation:
In this code, the function traverses every number in the given range sequentially to check whether it is divisible by any number from 2 to its square root. If a given input number has no divisors, it is considered prime and printed to the console; otherwise, the loop exits, terminating the operation.
Explore: Can I Get a Developer Job Without DSA? The Truth From 50+ Hired Developers
2. Find the sum of digits of a number (Loops and Conditionals)
Break the number into individual digits and add them all together.
function sumTotalOfDigits(num) {
let sum = 0;
while (num > 0) {
let digit = num % 10; // extract last digit
sum += digit; // add it to sum
num = Math.floor(num / 10); // remove last digit
}
console.log(sum);
}
sumTotalOfDigits(9876);
Output:
30
Explanation:
Observe the modulus operator % 10 here; it is included in this code to repeatedly extract the last digit of a number and add it back to a sum variable (which in this case is initialized to 0).
Once that is done, we move on to the next line, where we remove the last digit by dividing by 10, then use a built-in Math method to round it down to the nearest integer. This process continues until the number becomes 0. And at last, we get our resultant sum of all digits.
3. Find the second largest element in an array (Arrays and Strings)
Go through the array once and keep track of the largest and second largest values as you compare each element.
function secondLargestNumber(arr) {
if (arr.length < 2) {
console.log(“Invalid, number should contain at least 2 digit.”);
return;
}
let firstNum = -Infinity;
let secondNum = -Infinity;
for (let num of arr) {
if (num > firstNum) {
second = firstNum;
firstNum = num;
} else if (num > secondNum && num < firstNum) {
secondNum = num;
}
}
console.log(secondNum);
}
secondLargestNumber([10, 40, 30, 50, 20]);
Output:
40
Explanation:
Here, the ‘for of’ loop is used to traverse through each element in the array to keep track of the largest and second most significant numbers. The logic flows like this: if an element is larger than the current largest number in the array, it updates both the largest and the second-largest numbers.
If it’s between the largest and the second-largest, it updates only the second-largest. This way, the function effectively finds the second-largest element without unnecessary steps.
4. Reverse a string without using built-in functions (Arrays and Strings)
Swap characters from the start and end of the string, moving toward the middle, until the whole string is flipped.
function reverseString(str) {
let result = “”;
for (let i = str.length – 1; i >= 0; i–) {
result += str[i];
}
console.log(result);
}
reverseString(“hello”);
Output:
olleh
Explanation:
In this example, we run the for loop starting from the last character of the string, so we can iterate backwards and build a new string by adding each character in reverse order. This function traverses the string from end to start and appends each character, producing the reversed string as output.
5. Implement Linear Search (Searching)
Check each element one by one until you find the value you’re looking for.
function linearSearchOperation(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
console.log(`Element ${arr[i]} found at index ${i}`);
return;
}
}
console.log(“Element doesn’t exist.”);
}
linearSearchOperation([5, 8, 2, 9, 1], 9);
//and
linearSearchOperation([5, 8, 2, 9, 1], 4);
Output:
Element 9 found at index 3
Element doesn’t exist. // 4 is not present in the array
Explanation:
The search operation begins by checking each element of the array in sequence to determine whether any matches the target value. If a match is found, it prints the corresponding element along with its index. And if the loop completes without finding the target element, it prints that the number doesn’t exist.
6. Implement Binary Search on a sorted array (Searching)
Repeatedly divide the sorted array in half to quickly narrow down where the value is.
function binarySearchOperation(arr, target) {
let left = 0;
let right = arr.length – 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
console.log(`Element ${arr[mid]} found at index ${mid}`);
return;
}
else if (arr[mid] < target) {
left = mid + 1;
}
else {
right = mid – 1;
}
}
console.log(“Element not found inside the given array.”);
}
binarySearchOperation([2, 5, 8, 12, 16, 23, 38, 56], 23);
Output:
Element 23 found at index 5
Explanation:
Binary search is an algorithm that divides a sorted array in half to check whether the element matches the target value; this process repeats until the element is found or the array length becomes 1, indicating there is no more space left to search.
If the target is smaller than the middle element, it continues searching in the left half; if it is larger, it searches in the right half. And in this way, the search process becomes much faster than the linear search for sorted arrays, because in each loop, we traverse only half the previous array size.
7. Sort an array using Bubble Sort (Sorting)
Compare pairs of neighboring elements and swap them if they’re in the wrong order, repeating until the array is sorted.
function bubbleSortingNumbers(arr) {
let n = arr.length;
for (let i = 0; i < n – 1; i++) {
for (let j = 0; j < n – i – 1; j++) {
if (arr[j] > arr[j + 1]) {
// swapping elements with each other
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
console.log(“Resultant array in sorted order:”, arr);
}
bubbleSortingNumbers([64, 34, 25, 12, 22, 11, 90]);
Output:
Resultant array in sorted order: [11, 12, 22, 25, 34, 64, 90]
Explanation:
In this code, the function sorts the array by repeatedly comparing adjacent elements and swapping them if they are out of order. With every single loop execution, the biggest unsorted element bubbles up to the end of the array. And after completion of all loop runs, the input arrays get sorted in ascending order.
8. Sort an array using Insertion Sort (Sorting)
Take each element and insert it into its correct position among the already sorted elements before it.
function insertionSorting(arr) {
for (let i = 1; i < arr.length; i++) {
let key = arr[i];
let j = i – 1;
// Move the elements that are greater than the key element one position ahead
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j–;
}
arr[j + 1] = key;
}
console.log(“Resultant array in sorted order”, arr);
}
insertionSorting([12, 11, 13, 5, 6]);
Output:
Resultant array in sorted order: [5, 6, 11, 12, 13]
Explanation:
In this example, the function begins by treating an element as a key and placing it in the correct position among the already sorted elements on the left. It does this by gradually shifting all larger array elements to the right to make space for the key element. After repeating this process for every element, the input array gets sorted and printed to the console.
9. Find the factorial of a number using recursion (Basic Math and Number Problem)
Multiply the number by the factorial of the number just below it, repeating until you reach 1.
function findFactorial(n) {
let result = 1;
for (let i = 1; i <= n; i++) {
result *= i;
}
console.log(result);
}
findFactorial(6);
Output:
720
Explanation:
A factorial is the product of all the positive integers less than the given number, including itself. Here, we have calculated the factorial by multiplying the result variable by each number from 1 to the n parameter using a loop. After the loop completes, the result stores the final product value, which is printed to the console.
Also read: Maths for DSA: What Most Beginners Get Wrong
10. Print the Fibonacci series using recursion (Recursion)
Each number in the series is generated by adding the two numbers that came right before it.
function fibonacciRecursive(n) {
if (n === 0) return 0;
if (n === 1) return 1;
return fibonacciRecursive(n – 1) + fibonacciRecursive(n – 2);
}
Output:
0
1
1
2
3
5
8
Explanation:
This recursive Fibonacci function finds the nth Fibonacci number by checking if n is 0 or 1 (base cases) and returning 0 or 1 accordingly. For other values, it calls itself to calculate the previous two Fibonacci numbers and adds them, building the sequence recursively.
Also read: Mastering Recursion in Python: A Comprehensive Guide
11. Print a pyramid star pattern (Patterns)
Print stars in increasing numbers on each line to form a pyramid shape.
function pyramidStarPattern(rows) {
for (let i = 1; i <= rows; i++) {
let figure = “”;
// print the spaces
for (let j = 1; j <= rows – i; j++) {
figure += ” “;
}
// print the stars
for (let k = 1; k <= 2 * i – 1; k++) {
figure += “*”;
}
console.log(figure);
}
}
pyramidStarPattern(5);
Output:
*
***
*****
*******
*********
Explanation:
The code prints a pyramid shape by first adding spaces to align the stars, then printing stars for each row. Each subsequent row has two more stars than the previous one, forming a symmetrical pyramid. The spaces ensure the stars are properly centered.
12. Print a number triangle pattern (Patterns)
Print numbers in a specific order on each line to build a triangle shape.
function numberTrianglePattern(rows) {
for (let i = 1; i <= rows; i++) {
let num = “”;
for (let j = 1; j <= i; j++) {
num += j + ” “;
}
console.log(num);
}
}
numberTrianglePattern(5);
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Explanation:
The code prints a triangular pattern of numbers. Each row contains numbers from 1 up to the current row number. With each new row, one more number is added, creating a growing triangle of numbers in the output.
13. Insert a node at the end of a linked list (Linked List)
Walk through the list until you reach the last node, then attach the new node right after it.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
insertAtEnd(data) {
let newNode = new Node(data);
if (this.head === null) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next !== null) {
current = current.next;
}
current.next = newNode;
}
printList() {
let current = this.head;
let figure = "";
while (current !== null) {
figure += current.data + " -> ";
current = current.next;
}
figure += "null";
console.log(figure);
}
}
let list = new LinkedList();
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
list.printList();
Output:
10 -> 20 -> 30 -> null
Explanation:
The code creates a new node and adds it to the end of the linked list. If the list is empty, the new node becomes the head. Otherwise, it traverses the list until the last node and links the new node there. This way, elements are added sequentially to the end of the list.
14. Implement push and pop operations in a stack (Stack)
Push adds a new item to the top of the stack, and pop removes the item that’s currently on top.
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
if (this.items.length === 0) {
console.log(“Stack is empty, no items”);
return;
}
console.log(“Popped Item:”, this.items.pop());
}
printStack() {
console.log(“Stack:”, this.items.join(” -> “));
}
}
let stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
stack.printStack();
stack.pop();
stack.printStack();
Output:
Stack: 10 -> 20 -> 30
Popped Item: 30
Stack: 10 -> 20
Explanation:
The code uses an array to represent a stack. The push function adds an element to the end of the array, while the pop function removes the last element. This follows the Last In First Out (LIFO) principle, where the most recently added element is the first one to be removed.
15. Check if a number is a palindrome (Basic Math and Number Problems)
Reverse the number and check if it matches the original number.
function isPalindrome(num) {
let originalNum = num;
let reversedNum = 0;
while (num > 0) {
let digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num = Math.floor(num / 10);
}
if (originalNum === reversedNum) {
console.log(`${original} is a palindrome number`);
} else {
console.log(`${original} is not a palindrome number`);
}
}
isPalindrome(363);
isPalindrome(198);
Output:
363 is a palindrome number
198 is not a palindrome number
Explanation:
The code checks if a number is the same forwards and backwards. It reverses the number by extracting the last digit repeatedly and building a new reversed number. Finally, it compares the reversed number with the original. If they are equal, the number is a palindrome; otherwise, it is not.
Coding Problems in DSA: Quick Reference Table
Below is a categorized breakdown of the Coding Problems in DSA covered here, along with their difficulty and core concept:
| Problem Name | Category | Difficulty Level | Concept Used |
|---|---|---|---|
| Prime Numbers in a Range | Loops and Conditionals | Easy | Loop and Divisibility Check |
| Sum of Digits | Loops and Conditionals | Easy | Modulus and Division |
| Second Largest Element in an Array | Arrays and Strings | Easy | Single Pass Comparison |
| Reverse a String | Arrays and Strings | Easy | Two Pointer Swapping |
| Linear Search | Searching | Easy | Sequential Comparison |
| Binary Search | Searching | Easy | Divide and Conquer |
| Bubble Sort | Sorting | Easy | Swapping and Comparison |
| Insertion Sort | Sorting | Easy | Shifting and Insertion |
| Factorial Using Recursion | Basic Math and Number Problems | Easy | Recursion |
| Fibonacci Series Using Recursion | Recursion | Medium | Recursion |
| Pyramid Star Pattern | Patterns | Easy | Nested Loops |
| Number Triangle Pattern | Patterns | Easy | Nested Loops |
| Insert Node at End of Linked List | Linked List | Medium | Pointer Traversal |
| Push and Pop in a Stack | Stack | Easy | LIFO (Last In First Out) |
| Palindrome Number Check | Basic Math and Number Problems | Easy | Number Reversal |
Conclusion
Practicing Coding Problems in DSA regularly builds the kind of logical thinking that carries over into real projects and interviews. Starting with simple patterns, loops, and searches lays the groundwork for tackling recursion, linked lists, and sorting with more clarity. Each problem you solve adds a small piece to the bigger picture of how code actually works.
FAQs
1. How many Coding Problems in DSA should a beginner solve daily?
One or two a day is enough. Consistency and problem quality matter more than speed.
2. Which topic should I start with first?
Loops and arrays. They’re the easiest entry point into Coding Problems in DSA.
3. Do I need a specific programming language to solve these?
No. The logic behind Coding Problems in DSA stays the same across languages, but Python or JavaScript are good ones to start with.
4. Are these problems enough for interview preparation?
They’re a good start, but interview-level Coding Problems in DSA go deeper into optimization.
5. What’s the best way to practice these problems?
Write the logic on paper first, then code it. Don’t jump straight to solutions.



Did you enjoy this article?