How NOT to Use Chat-GPT for Solving Coding Problems

We have to be careful while solving coding-related problems using Chat-GPT because Chat-GPT does not claim 100% Accuracy for Generated Output. Suppose we are given a Problem Statement in Story Format / In-Sufficient Data / Wrong Examples / Wrong I/O. In That Case, Chat-GPT will surely Generate the Wrong Answer for an Input.

Case 1: Not providing sufficient information to generate the desired output

Let’s take an example of a popular problem Print Spiral Matrix. The task is to print a spiral matrix but in Anti-Clock-Wise Direction. So let’s give Chat-GPT a command to Print the Spiral Matrix in Anti-Clock Wise Direction.

Query asked

Given an m x n matrix, return all elements of the matrix in spiral order in an anticlockwise direction. Input: matrix = [[1,2,3],[4,5,6],[7,8,9]].

Answer generated by Chat-GPT:

Answer generated by Chat-GPT for Not providing sufficient information to generate the desired output

Now let’s try to run the generated C++ code against the above input with the expected output as [1 4 7 8 9 6 5 2 3]

C++




#include <iostream>
#include <vector>
  
using namespace std;
  
vector<int> AntispiralOrder(vector<vector<int>>& matrix) {
    int top = 0, bottom = matrix.size() - 1, left = 0, right = matrix[0].size() - 1;
    int dir = 1;
    vector<int> result;
  
    while (top <= bottom && left <= right) {
        if (dir == 1) {
            for (int i = left; i <= right; i++) {
                result.push_back(matrix[top][i]);
            }
            top++;
  
            for (int i = top; i <= bottom; i++) {
                result.push_back(matrix[i][right]);
            }
            right--;
  
            for (int i = right; i >= left; i--) {
                result.push_back(matrix[bottom][i]);
            }
            bottom--;
  
            for (int i = bottom; i >= top; i--) {
                result.push_back(matrix[i][left]);
            }
            left++;
        }
        dir *= -1;
    }
  
    return result;
}
  
int main() {
    vector<vector<int>> matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  
    vector<int> result = AntispiralOrder(matrix);
  
    for (int i = 0; i < result.size(); i++) {
        cout << result[i] << " "; //1 4 7 8 9 6 5 2 3
    }
  
    return 0;
}


Output

1 2 3 6 9 8 7 4 5 

Expected Output

1 4 7 8 9 6 5 2 3

As we can see that we are getting the wrong answer for the above Inputs.

Why Chat-GPT is producing incorrect output in this case?

We have provided insufficient Information this is the main reason Why Chat-GPT is giving us the wrong Output:

Let’s see what should be the proper format for the above Example to get the expected answer:

Write a program to print a spiral matrix in an anticlockwise direction.
Command: Example: Input: arr = [[1,2,3],[4,5,6],[7,8,9]]; Output: arr = [1,4,7,8,9,6,3,2,5]

Generated Code:

C++




#include <iostream>
#include <vector>
  
using namespace std;
  
vector<int> spiralOrder(vector<vector<int>>& matrix) {
    vector<int> res;
    if (matrix.empty()) {
        return res;
    }
    int m = matrix.size();
    int n = matrix[0].size();
    int i = 0, j = n-1;
    while (i < j && i <= m-i-1 && j >= n-j && j <= j-i) {
        for (int k = i; k <= m-i-1; k++) {
            res.push_back(matrix[k][i]);
        }
        for (int k = i+1; k <= j; k++) {
            res.push_back(matrix[m-i-1][k]);
        }
        if (i < j && i <= m-i-2 && j >= n-j) {
            for (int k = m-i-2; k >= i; k--) {
                res.push_back(matrix[k][j]);
            }
        }
        if (i < j && i <= m-i-1 && j >= n-j+1) {
            for (int k = j-1; k >= i+1; k--) {
                res.push_back(matrix[i][k]);
            }
        }
        i++;
        j--;
    }
    if (i == j) {
        res.push_back(matrix[i][i]);
    }
    return res;
}
  
int main() {
    vector<vector<int>> arr = {{1,2,3},{4,5,6},{7,8,9}};
    vector<int> res = spiralOrder(arr);
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << " ";
    }
    cout << endl;
    return 0;
}


Output

1 4 7 8 9 6 3 2 5 

As we can see that we are getting the Correct answers for the above Inputs.

Case 2: Chat-GPT is still new and learning

Though Chat-GPT is able to produce almost every answer and even correct it to most extent, still it is very new and learning. So there might be cases where it shows you incorrect answers, or fail to give you the correct answer the first time. 

For example, 

Here’s how Chat-GPT failed to provide complexity analysis for Fibonacci Number Recursion approach the first time.

how Chat-GPT failed to provide complexity analysis for Fibonacci Number Recursion approach the first time

But gave the correct output the second time.

how Chat-GPT gave the correct output the second time

How to use Chat-GPT to solve Coding Problems?

Its 2023 and AI Models are on the rise, even for a simple task. Then why not use it for solving Coding problems? One such popular model is Chat-GPT. Chat-GPT can be a valuable resource for students looking to solve coding-related problems. It can provide quick answers to simple questions about syntax, algorithms, and data structures, which can save students time and help them better understand programming concepts. Chat-GPT can also assist students with more complex coding problems by suggesting potential solutions, providing examples, or explaining coding concepts in a clear and concise way.

By using Chat-GPT, students can learn at their own pace and in a format that suits their learning style. They can ask questions and receive answers immediately, without having to wait for a teacher or tutor to become available.

How to use Chat-GPT to solve Coding Problems?

Similar Reads

How to use Chat-GPT efficiently to solve Coding Problems?

Chat-GPT is Phenomenal when it comes to problem-solving. Students can ask Chat-GPT-specific questions related to coding and programming, and it can provide step-by-step guidance on how to solve coding problems. Additionally, Chat-GPT can offer coding tips, tricks, and best practices, helping students to improve their coding skills and knowledge. With its vast knowledge base and ability to quickly process and analyze data, Chat-GPT is a valuable resource for students seeking coding help....

How to form Query correctly to get Expected Answer From Chat-GPT?

It’s observed that If one does not mention the clear Problem statement and all other required data-set which is required to solve the problem statement then Chat-GPT may give the wrong answer as Output....

How to use Chat-GPT to solve different types of Coding Problems?

1. Data Structure and Algorithm (DSA) Related Problems...

How NOT to Use Chat-GPT for Solving Coding Problems

...

Why can’t you trust Chat-GPT to generate the correct output every time?

...

What to do when Chat-GPT fails to solve your Coding Problems?

...

Should we use Chat-GPT to solve Coding Problems?

...