ChatGPT vs LeetCode

Is ChatGPT that good and can really help developers solve tasks? Today we’re going to find out it by facing the LeetCode task with ChatGPT.

Andrii Drozdov
7 min readFeb 8, 2023

Hello, my favourite bit lovers.

For those who are not aware, what is ChatGPT — is a chatbot developed by the company named OpenAI. It has built AI features that allow t any question, including the answer to “The answer to life, the universe, and everything”. BTW, it’s 42.

Disclaimer

I do not encourage you to cheat on LeetCode and this article is just a fun experiment about ChatGPT features. I think that testing your skills on LeetCode or any other platform should be done exclusively by you, without any external help.

But can ChatGPT write code?

Writing code

Hmm, let’s ask it to write a simple code, Singleton Pattern in JavaScript.

Interesting… ChatGPT definitely can implement Singleton Pattern, and a small but efficient feature with code highlighting. Let’s straight away notice an elephant in the room, for some reason ChatGPT thinks that this code is Kotlin, let’s ignore it for now, and assume it’s just a display bug.

But it’s one thing to talk about implementing a common, widely spread code base and a completely different is to solve LeetCode issues.

LeetCode

As all of you are aware, LeetCode has tasks (problems) with different difficulties, we are going to test ChatGPT to work with every one of them. Unfortunately, ChatGPT can only work with texts, so we are going to ignore problems with image explanations.

Easy difficulty problem: “Palindrome Number”

On my LeetCode account, I have a task, named “9. Palindrome Number“ unsolved yet. Let’s see how ChatGPT will handle it.

Here’s a task description:

Given an integer x, return true if x is a palindrome, and false otherwise.

Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Let’s ask ChatGPT to solve this.

So, as you see ChatGPT managed to generate a syntax-correct working code for us. Code results are:

function isPalindrome(x) {
if (x < 0) return false;
let original = x;
let reverse = 0;
while (x > 0) {
reverse = reverse * 10 + (x % 10);
x = Math.floor(x / 10);
}
return original === reverse;
}

Not, let’s see if this code will pass the LeetCode test suits:

YES!

It passed it. And regarding memory efficiency — pretty efficient.

We can try to ask the chatbot to optimise our code base, let’s see how it will handle optimisation if it’s even possible.

“It’s f**king brilliant!”

Here’s a code produced by ChatGPT:

function isPalindrome(x) {
if (x < 0 || (x % 10 === 0 && x !== 0)) return false;
let reverse = 0;
while (x > reverse) {
reverse = reverse * 10 + (x % 10);
x = Math.floor(x / 10);
}
return x === reverse || x === Math.floor(reverse / 10);
}

If we will look at efficiency, so logically we have an early return, which may help us with performance optimisation. What LeetCode thinks about this optimisation?

As you see, from the LeetCode point of view, the runtime is decreased from 182ms to 171ms, so yeah, “10 points to ChatGPT.

Notice: I had to play with the chatbot and do some tweaking, so it produced a working code after optimisation, because, originally it returned code that did not pass testing suits.

Medium difficulty problem: “Longest Substring Without Repeating Characters”

The easy task is an easy task, let’s see how ChatGPT with a more complex task.

Once again, on my LeetCode account, I have a medium difficult task name “Longest Substring Without Repeating Characters”, here it’s a description:

Given a string s, find the length of the longest substring without repeating characters.

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

Let’s see what ChatGPT will produce:

Here’s a code:

var lengthOfLongestSubstring = function(s) {
let map = new Map();
let left = 0;
let maxLength = 0;
for (let i = 0; i < s.length; i++) {
if (map.has(s[i]) && map.get(s[i]) >= left) {
left = map.get(s[i]) + 1;
}
map.set(s[i], i);
maxLength = Math.max(maxLength, i - left + 1);
}
return maxLength;
};

And now we will submit it to LeetCode:

Do you see? LeetCode accepted it and it’s pretty efficient, BTW. Lovely!

Hard difficulty task “Median of Two Sorted Arrays”

Let’s see how ChatGPT will handle the most complex problem on LeetCode.

Here’s a description of the task:

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)).

Example 1:

Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.

Example 2:

Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

Let’s feed this to the chatbot.

This time it produced a ton of code, here is the code:

var findMedianSortedArrays = function(nums1, nums2) {
let m = nums1.length, n = nums2.length;
if (m > n) return findMedianSortedArrays(nums2, nums1);
let imin = 0, imax = m, halfLen = Math.floor((m + n + 1) / 2);
while (imin <= imax) {
let i = Math.floor((imin + imax) / 2);
let j = halfLen - i;
if (i < m && nums2[j-1] > nums1[i]) {
imin = i + 1;
} else if (i > 0 && nums1[i-1] > nums2[j]) {
imax = i - 1;
} else {
let maxLeft = 0;
if (i === 0) {
maxLeft = nums2[j-1];
} else if (j === 0) {
maxLeft = nums1[i-1];
} else {
maxLeft = Math.max(nums1[i-1], nums2[j-1]);
}
if ((m + n) % 2 === 1) return maxLeft;
let minRight = 0;
if (i === m) {
minRight = nums2[j];
} else if (j === n) {
minRight = nums1[i];
} else {
minRight = Math.min(nums1[i], nums2[j]);
}
return (maxLeft + minRight) / 2;
}
}
return 0;
};

Now let us see if LeetCode will accept it:

WOW. Once again, LeetCode accepted code produced by ChatGPT and it’s pretty efficient.

Conclusion

You can notice that ChatGPT show itself as unbelievably efficient and can solve easy to hard problems on LeetCode.

Unfortunately, can only work with text, which means that we cannot insert pictures.

Firstly we taught AI to drive a car, then pain pictures, then generate texts, and we come when it can generate a code. Yes, it is still pretty far away for AI to generate whole applications, and do something more complex, but the direction is already visible.

ChatGPT is an awesome tool and it definitely can help a developer with day-by-day work.

That’s it for today. Hope you had a fun day.

Don’t forget to follow me for more!

Your bit. 01101100 01101111 01110110 01100101

--

--

Andrii Drozdov
Andrii Drozdov

Written by Andrii Drozdov

CTO & developer, to whom every breath, is an injection of bits into CPU of my life

No responses yet