Open In App

How To Debug Your Code | For Beginners

Last Updated : 22 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Debugging is a process of finding errors, mistakes, or bugs in the code so that code gives the desired output. Debugging involves pointing out and resolving the errors or issues that cause the error to behave unexpectedly or produce wrong output by reviewing the code carefully line by line, testing the code with given inputs and comparing the result with the expected output, printing statements in between the code to track the flow of the code, and tracking the values of variables during runtime and many other ways that we will discuss further in detail.

How To Debug Your Code | For Beginners

What is the importance of Debugging in coding?

Debugging is important along with coding as it ensures that any error or bug is identified before submitting the code and is resolved so that the code runs smoothly without producing any error and provides the correct and expected output.

  • Identifying and fixing the errors: DSA and competitive coding problems require implementing complex logic and use of various data structures to solve the problem. Logical errors such as implementing incorrect logic or not using the correct data structure can lead to incorrect output. Debugging is required to identify and resolve such issues and ensure that the code produces the correct output.
  • Testing edge cases: DSA and competitive problems have some edge case that needs to be handled correctly, debugging helps the coder to test and verify if their code if working fine for edge cases or not, ensuring that the code is correct.
  • Understanding the flow of code: Debugging helps in understanding the flow of code by reviewing the code line by line and allows the coder to track variable values at each instance and gives insight into the flow of code in which it is executing which helps in finding and resolving issues.
  • Improve problem-solving skills: We can say that debugging is also a part of problem-solving. By debugging and fixing issues, the coders learn from mistakes and improve their problem-solving skills.
  • Optimizing time and space complexity: Debugging can help to identify the areas in the code that can be optimized, such as repetitive calculations or eliminating the auxiliary space used and making the code more optimized.

How to perform debugging for Coding issues:

1. Review the code:

To debug the code, you should begin by going through the code line by line and try to identify the errors or issues with logic. While reviewing the code line by line, you should check if the syntax is written according to the rules of the programming language, the algorithmic logic used in the code to solve the particular problem, and the data structures used in the code.

Example:

Given an array of integers, the idea is to find the maximum element of an array.

Approach: The simplest approach to solve this problem is to initialize a max variable to store the result and iterate the given array and update the max variable with the maximum value so far.

Below is the code to solve the problem:

C++
// CPP program to find minimum element
// in an array.
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int arr[] = { 12, 1234, 45, 67, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int res = 0;
    for (int i = 1; i < n; i++)
        res = max(res, arr[i]);
    cout << "Maximum element of array: " << res;
    return 0;
}
Java
// Java program to find the maximum element
// in an array.

import java.util.Arrays;

public class MaxElementInArray {
    public static void main(String[] args) {
        // Initialize the array
        int[] arr = {12, 1234, 45, 67, 1};
        
        // Get the length of the array
        int n = arr.length;
        
        // Initialize the result variable to store the maximum element
        int res = 0;

        // Iterate through the array to find the maximum element
        for (int i = 1; i < n; i++) {
            // Update the result with the maximum value between current element and the result
            res = Math.max(res, arr[i]);
        }

        // Print the maximum element of the array
        System.out.println("Maximum element of array: " + res);
    }
}
Python3
# Python program to find maximum element
# in an array.

arr = [12, 1234, 45, 67, 1]
n = len(arr)
res = 0

for i in range(1, n):
    res = max(res, arr[i])

print("Maximum element of array:", res)
C#
using System;

class Program
{
    static void Main(string[] args)
    {
        int[] arr = { 12, 1234, 45, 67, 1 };
        int res = int.MinValue; // Initialize with the minimum possible value
        foreach (int num in arr)
        {
            res = Math.Max(res, num);
        }
        Console.WriteLine("Maximum element of array: " + res);
    }
}
Javascript
let arr = [12, 1234, 45, 67, 1];
let res = arr[0]; // Initialize result with the first element of the array

for (let i = 1; i < arr.length; i++) {
    res = Math.max(res, arr[i]); // Update result if a larger element is found
}

console.log("Maximum element of array: " + res);

Output
Maximum element of array: 1234

The above code is written in C++ language. If we review this code line by line, we can see required header files are there in the code. main() function is present in the above code and all rules of programming in C++ are followed.

2. Test with sample inputs:

Before starting to code, make sure that you understand the problem clearly. Construct an algorithm to solve the problem and then start to code. Online coding sites give sample test cases for you to understand the problem better. Test your code for given sample test cases and make some sample edge case input test cases and check for the produced output and expected output. If the outputs do not match, that means there might be some logical error and then you can proceed to rectify your algorithm and debug the code.

Example:

For the above example, We have created a sample test case and we have tested the code for the sample input test case and the code is working fine. But we need to take care of the edge/corner cases. One of the edge cases for the above example can be if there exists only negative integers in the array. Let’s create such sample input and test our code for it.

Sample Input: -12, -1234, -45, -67, -1

Below is the Implementation of the Code:

C++
// CPP program to find minimum element
// in an array.
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int arr[] = { -12, -1234, -45, -67, -1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int res = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] > res)
                res = arr[i];
        }
    cout << "Maximum element of array: " << res;
    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        int[] arr = { -12, -1234, -45, -67, -1 };
        int n = arr.length;
        int res = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] > res)
                res = arr[i];
        }

        System.out.println("Maximum element of array: " + res);
    }
}
//This code is contributed by Monu.
Python3
# Initialize the array
arr = [-12, -1234, -45, -67, -1]
n = len(arr)  # Get the length of the array
res = 0  # Initialize result as 0

# Iterate through the array to find the maximum element
for i in range(n):
    if arr[i] > res:
        res = arr[i]

# Print the maximum element of the array
print("Maximum element of array:", res)
JavaScript
// This code finds the maximum element in an array

// Define the array
const arr = [-12, -1234, -45, -67, -1];
const n = arr.length;
let res = 0;

// Iterate through the array to find the maximum element
for (let i = 0; i < n; i++) {
    if (arr[i] > res) {
        res = arr[i];
    }
}

// Print the maximum element
console.log("Maximum element of array: " + res);

Output
Maximum element of array: 0

The code for the above example is giving the wrong output. When we reviewed the code and tested the code on the sample test case, it looks like the code is working fine but this is not the case. Here comes dry run into the picture.

3. Dry run the code:

Dry run is a technique used by programmers to visualize the execution of a code on paper and understand the logic of the code without actually running it on the computer. Identify the test case/edge case for which the code fails. Use the values of sample input for which the code fails and try to write the execution of the code on paper and update the values of variables for each iteration according to the conditions in the code and you can identify where the code is given unexpected output and then you can proceed to debug the code.

Here are some steps to follow when dry-running an algorithm:

  • Understand the problem: Before you can dry run an algorithm, you need to fully understand the problem you are trying to solve. Make sure you know what input the algorithm should take, what output it should produce, and what constraints or requirements apply.
  • Break down the algorithm: Next, break down the algorithm into smaller steps or components. Identify the inputs, outputs, and operations performed at each step. This will help you understand how the algorithm works and how data flows through it.
  • Walk through the algorithm: Once you have broken down the algorithm, walk through each step in the algorithm manually. Use pen and paper or a whiteboard to keep track of the values of variables and data structures at each step. Make sure you understand how each step contributes to the overall goal of the algorithm.
  • Test with sample inputs: To validate your understanding of the algorithm, test it with sample inputs. Choose inputs that cover a wide range of cases and edge cases, including cases where the input is invalid or unexpected. Walk through the algorithm with these inputs to make sure the output matches what you expect.
  • Optimize the algorithm: Finally, look for ways to optimize the algorithm. Are there any redundant steps or operations that can be eliminated? Are there any data structures that can be simplified or optimized? Look for ways to improve the algorithm’s performance without sacrificing correctness.

4. Review algorithmic logic:

The above code gives wrong output, we need to read the problem statement again and understand the problem statement clearly. Analyze the input and output for which the code fails.

C++
// CPP program to find minimum element
// in an array.
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int arr[] = { -12, -1234, -45, -67, -1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int res = 0;
    for (int i = 1; i < n; i++)
        res = max(res, arr[i]);
    cout << "Maximum element of array: " << res;
    return 0;
}
Java
// Importing the Arrays class from java.util package
import java.util.Arrays; 

public class Main {
    public static void main(String[] args) {
        // Initialize the array
        int[] arr = { -12, -1234, -45, -67, -1 };
        int n = arr.length; // Get the length of the array
        int res = 0; // Initialize the result variable to 0

        // Iterate through the array to find the maximum element
        for (int i = 0; i < n; i++)
        // Update the result if a larger element is found
            res = Math.max(res, arr[i]); 

        // Print the maximum element of the array
        System.out.println("Maximum element of array: " + res);
    }
}
//This code is contributed by UTkarsh
Python3
# code
import math

# Initialize the array
arr = [-12, -1234, -45, -67, -1]
# Get the length of the array
n = len(arr)
# Initialize the result variable to 0
res = 0

# Iterate through the array to find the maximum element
for i in range(n):
    # Update the result if a larger element is found
    res = max(res, arr[i])

# Print the maximum element of the array
print("Maximum element of array:", res)
JavaScript
// Initialize the array
const arr = [-12, -1234, -45, -67, -1];
const n = arr.length; // Get the length of the array
let res = 0; // Initialize the result variable to 0

// Iterate through the array to find the maximum element
for (let i = 0; i < n; i++) {
    // Update the result if a larger element is found
    res = Math.max(res, arr[i]); 
}

// Print the maximum element of the array
console.log("Maximum element of array: " + res);

Output
Maximum element of array: 0

The problem with this approach is that it initializes the variable res = 0, assuming that all elements in the array are non-negative. However, this is not always the case, as the array can also contain negative elements. In such a case, the res variable should be initialized to the first element of the array.

  • To fix this issue, we can modify the algorithm to initialize the variable res to the first element of the array and start the loop from index 1. Here is the modified code:
C++
// CPP program to find minimum element
// in an array.
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int arr[] = { -12, -1234, -45, -67, -1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int res = arr[0];
    for (int i = 1; i < n; i++)
        res = max(res, arr[i]);
    cout << "Maximum element of array: " << res;
    return 0;
}
Java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] arr = { -12, -1234, -45, -67, -1 };
        int res = arr[0];
        for (int i = 1; i < arr.length; i++)
            res = Math.max(res, arr[i]);
        System.out.println("Maximum element of array: " + res);
    }
}
Python3
# Python program to find minimum element
# in an array.

arr = [-12, -1234, -45, -67, -1]
n = len(arr)
res = arr[0]
for i in range(1, n):
    res = max(res, arr[i])
print("Maximum element of array:", res)
JavaScript
// JavaScript program to find maximum element
// in an array.

// Array declaration
const arr = [-12, -1234, -45, -67, -1];

// Calculate the length of the array
const n = arr.length;

// Initialize the result variable with the first element of the array
let res = arr[0];

// Loop through the array to find the maximum element
for (let i = 1; i < n; i++) {
    // Update res if the current element is greater than res
    res = Math.max(res, arr[i]);
}

// Print the maximum element of the array
console.log("Maximum element of array: " + res);

Output
Maximum element of array: -1

5. Simplify the code:

The idea is to simplify the above code so that the code becomes easy to debug. To simplify the above code we can divide it into functions.

  • A function to find the maximum element of an array.
  • The main function to initialize the array, call the maximum element function, and output the result.

Below is the simplified code:

C++
#include <bits/stdc++.h>
using namespace std;

// Function to find the maximum element of an array
int findMax(int arr[], int n)
{
    // initialize to the first element
    int res = arr[0];
    for (int i = 1; i < n; i++)
        res = max(res, arr[i]);
    return res;
}

// Main function to initialize the array, call the maximum
// element function, and output the result
int main()
{
    int arr[] = { -12, -1234, -45, -67, -1 };
    int n = sizeof(arr) / sizeof(arr[0]);

    int maxElement = findMax(arr, n);

    cout << "Maximum element of array: " << maxElement;

    return 0;
}
Java
public class Main {
    // Function to find the maximum element of an array
    public static int findMax(int[] arr, int n) {
        // initialize to the first element
        int res = arr[0];
        for (int i = 1; i < n; i++)
            res = Math.max(res, arr[i]);
        return res;
    }

    // Main function to initialize the array, call the maximum
    // element function, and output the result
    public static void main(String[] args) {
        int[] arr = { -12, -1234, -45, -67, -1 };
        int n = arr.length;

        int maxElement = findMax(arr, n);

        System.out.println("Maximum element of array: " + maxElement);
    }
}

Output
Maximum element of array: -1

Tips and Tricks to Debug the Code:

1. Print debug statements:

Inserting print statements in our code can help us understand the behavior of the code and identify the problem. We can try printing the statements that can help us to track the flow of the code and printing values of variables or the value of the result at each iteration in the loop, or other variables that can help us track the execution of our code.

2. Use Online IDE debugger:

A debugger allows us to step through your code and check the value of the variables at each step. It can help us identify the problem, find the cause of the error, and fix it.

Challenges in debugging DSA and Competitive coding problems:

1. Lack of clarity of problem specification:

Generally, DSA and competitive problems are given in complex problem statements that might not be clear and is challenging to understand the problem clearly and verify the correctness of the code.

2. Limited visibility of internal implementation of Data Structures:

Problem-solving involves the use of different data structures such as arrays, Linked Lists, Trees, Graphs, and many more. Debugging the code that uses several complex data structures can be challenging as the internal implementation of these data structures is not visible at runtime.

3. Complex algorithmic logic:

DSA and competitive programming problems can be very complex and the programmer needs to build complex algorithms and logic to solve the problem it can be challenging to trace the flow of the code as it may involve multiple steps and conditions that need to be verified carefully.

4. Large input size:

DSA and competitive coding problems may have large input sizes and debugging such code may consume both time and resources.

5. Lack of accurate error messages:

Some DSA problems may not provide exact error messages making it challenging to identify the exact cause of issues that require extensive code analysis to identify the root cause of errors.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads