Apply Now Apply Now Apply Now
header_logo
Post thumbnail
DATA STRUCTURE

Basic Coding Problems in DSA for Beginners in 2025

By Abhishek Pati

DSA (Data Structures and Algorithms) can be overwhelming for most individuals who begin their coding or programming journey. But is DSA extremely difficult, or is it just a common misconception we have developed?

Yes, DSA is indeed a challenging computer science topic to master; however, if an appropriate learning path is followed, then learning DSA concepts and solving coding challenges becomes relatively more straightforward. And for that, starting with basic coding problems in DSA is highly recommended, as it allows beginners to analyze, plan, and implement effective solutions without losing confidence.

In this blog, we’ll walk through some of the best beginner-friendly DSA problems. So, let’s begin.

Table of contents


  1. Key Concepts to Master for Solving DSA Problems
  2. 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)
  3. Conclusion
  4. FAQs
    • What is DSA, and why should beginners learn it?
    • How many problems should a beginner practice daily?
    • Do I need to memorize formulas and algorithms?

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: Here, we have used JavaScript as the primary language to explore the following fundamental DSA problems. Other programming languages, such as C++, Java, or Python, can also be used to solve the same problems (the logic is the same across all programming languages; only the syntax differs).

Strengthen your programming skills with our comprehensive and affordable DSA course: DSA for Programmers Course

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)

(Code)

{

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. So let’s say that if a given input number has no divisors, it is considered a prime number and printed to the console; otherwise, the loop exits, terminating the operation.

MDN

2. Find the sum of digits of a number (Loops and Conditionals)

(Code)

{

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 following line, where we completely remove the last digit from the number 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)

(Code)

{

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. So the logic flows like this: if a specific element is larger than the current largest number in the array, it updates both the largest and the second-largest numbers.

And if it’s between the largest and the second-largest, it only updates the second-largest. In this way, the above function effectively finds out the second-largest element without performing any unnecessary steps.

4. Reverse a string without using built-in functions (Arrays and Strings)

(Code)

{

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 have run the for loop with the last character of the string as the initial value, so we can execute the loop backwards and create a new resultant 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)

(Code)

{

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 the number matches, 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)

(Code)

{

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 the sorted array in half to check whether the element matches the target value, and this process is repeated until the element is found or the array length becomes 1, indicating there is no more space left to search. 

So if the target is smaller than the middle array 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)

(Code)

{

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 performs the sort operation by repeatedly comparing adjacent array 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)

(Code)

{

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 assigning it the correct position among the already-sorted elements on the left. It performs this task by gradually shifting all the larger array elements to the right side to create 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)

(Code)

{

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 results store the final product value, which is printed to the console.

Also read: Maths for DSA: What Most Beginners Get Wrong [2025 Guide]

10. Print the Fibonacci series using recursion (Recursion)

(Code)

{

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 [2025]

11. Print a pyramid star pattern (Patterns)

(Code)

{

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 adding spaces first to align the stars and 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)

(Code)

{

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)

(Code)

{

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)

(Code)

{

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)

(Code)

{

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.

If you want to become a full-stack developer and gain practical experience building real-world applications, HCL GUVI’s IITM Pravartak Certified MERN Full Stack Development Course is the ideal choice. Learn to work with essential technologies like Git, MongoDB, Express, React, and Node.js under the mentorship of industry experts. Take your first step toward a successful tech career—enroll today and start learning!

Conclusion

These basic DSA problems help beginners build a strong foundation in loops, arrays, strings, searching, sorting, recursion, and data structures like stacks and queues. Practicing them improves problem-solving and logical thinking skills and prepares you for interviews and real-world programming tasks. Mastering these basics makes learning advanced DSA much easier.

FAQs

What is DSA, and why should beginners learn it?

DSA helps store and process data efficiently. Learning improves problem-solving and coding skills and prepares for interviews.

How many problems should a beginner practice daily?

Start with 1–3 problems daily, focus on understanding the logic, then gradually increase difficulty.

MDN

Do I need to memorize formulas and algorithms?

No, understanding the logic is more important. Practice will make common patterns familiar over time.

Success Stories

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Loading...
Get in Touch
Chat on Whatsapp
Request Callback
Share logo Copy link
Table of contents Table of contents
Table of contents Articles
Close button

  1. Key Concepts to Master for Solving DSA Problems
  2. 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)
  3. Conclusion
  4. FAQs
    • What is DSA, and why should beginners learn it?
    • How many problems should a beginner practice daily?
    • Do I need to memorize formulas and algorithms?