DEV Community


Posted on

Leetcode with ChatGPT

I asked ChatGPT to solve leetcode hard DP problems. The results were well, interesting ...

I started with Frog Jump and the results were promising. Frog Jump is a classic DP problem that is solved by memoization and backtracking. ChatGPT produced python code that could easily be ported into leetcode.

Frog Jump Python

The solution had a trivial bug but passed most testcases. Given the signature of the helper recursive function,

def canCrossHelper(position, jump)

the initial call should have been canCrossHelper(0,0) and not canCrossHelper(0,1). After fixing the bug, the solution was faster than 90+% of Python solutions. Not bad.

I then tried something more convoluted, Count ways to make array with product. This is a complex problem that applies prime factorization and combinatorics on top of DP techniques. The results were less than stellar.

Count ways Java

It produced a DP solution with recurrence relation

dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - 1])

which only sounds correct, but is infact wrong. Even after prompting to revise the solution, nothing fundamental changed.

These cases told me all I needed to know about interviewing with AI. Errors are to be expected since AI is only as good as the training data. ChatGPT already has the disclaimer in the footnote ChatGPT can make mistakes. Consider checking important information. Here is the link to the full chat.

A candidate could have some success from asking ChatGPT in a live interview, essentially cheating. Or they could master algorithms with confidence. A resource I recommend to is Ace the technical interviews courses, DP course coming soon. They help you build intuition around algorithmic techniques and reduce time to optimal solution. I'm biased because I wrote them but they free at the moment and feedback is welcome.

Top comments (0)