DEV Community

Cover image for I Asked Phind and Copilot to Solve a LeetCode-Like Interview Exercise—Their Solutions Surprised Me
Cesar Aguirre
Cesar Aguirre

Posted on • Originally published at canro91.github.io

I Asked Phind and Copilot to Solve a LeetCode-Like Interview Exercise—Their Solutions Surprised Me

I originally posted this post on my blog.


Did AI kill the tech interview?

Truth is hiring and interviewing have been broken for years. There wasn't much left to kill.

In over 10 years, I've witnessed all types of interviews: casual conversations, interrogation-like conversations with rapid-fire questions, take-home coding exercises, and the infamous LeetCode exercises.

I asked Phind and Copilot to solve an interview exercise. I might or might not have been asked that exercise when applying to a FAANG.

Here's the "made-up" problem:

You're given a string containing the words "one" and "two", and the symbols "+" and "-" representing a math expression. Write a C# function called "Evaluate" to evaluate that expression after replacing "one" with 1 and "two" with 2. Assume the underlying expression is well-formed. For example, "one+one" should return 2, "two-two-one-two" should return -3, and "one+two-one-one+two+one" should return 4.

I'm using that exact same problem statement as a prompt for both Phind and Copilot. I know...I could have a more advanced prompt.

In this corner, with over 10 years of C# experience, me:

Here's my "pure string evaluation" solution:

private int Evaluate(string str)
{
    var first = str[..3];
    var result = first == "one" ? 1 : 2;

    var index = 3;
    var lastOperation = '+';
    while (index < str.Length)
    {
        var substring = str.Substring(index);
        if (substring.StartsWith('+'))
        {
            lastOperation = '+';
            index++;
        }
        else if (substring.StartsWith('-'))
        {
            lastOperation = '-';
            index++;
        }
        else if (substring.StartsWith("one"))
        {
            result = lastOperation == '+' ? result + 1 : result - 1;
            index += 3;
        }
        else if (substring.StartsWith("two"))
        {
            result = lastOperation == '+' ? result + 2 : result - 2;
            index += 3;
        }
    }

    return result;
}
Enter fullscreen mode Exit fullscreen mode

OK, that's ugly and has lots of repetition.

I looped through the string to identify if I was visiting a number or a symbol and stored the last known number and operation.

Nothing fancy! Probably I wouldn't have hired myself with that solution. But, hey, "it works on my machine."

And, let's see what Phind and Copilot do. Place your bets...Drum rolls...

In this corner, with its "largest and most performant model to date," Phiiiiind:

Here's Phind's exact solution (here's my conversation but I don't know how long it's stored though):

public static class WordNumberEvaluator
{
    private const string ONE = "one";
    private const string TWO = "two";

    /// <summary>
    /// Evaluates a mathematical expression containing word numbers 'one' and 'two'.
    /// </summary>
    /// <param name="expression">The expression to evaluate</param>
    /// <returns>The numerical result of the expression</returns>
    public static int Evaluate(string expression)
    {
        // Replace word numbers with digits
        string processed = expression
            .Replace(ONE, "1")
            .Replace(TWO, "2");

        // Use DataTable.Compute to evaluate the expression
        var dataTable = new DataTable();
        var result = dataTable.Compute(processed, String.Empty);

        return Convert.ToInt32(result);
    }
}

public class Program
{
    public static void Main()
    {
        Console.WriteLine(WordNumberEvaluator.Evaluate("one+one"));       // Outputs: 2
        Console.WriteLine(WordNumberEvaluator.Evaluate("two-two-one-two")); // Outputs: -3
        Console.WriteLine(WordNumberEvaluator.Evaluate("one+two-one-one+two+one")); // Outputs: 4
    }
}
Enter fullscreen mode Exit fullscreen mode

Phind surprised me a lot.

  1. It used a DataTable to evaluate expressions. I didn't know DataTable could do that. I wouldn't have thought about that in a real interview.
  2. It added docstring comments and two comments to explain its solution.

But that didn't surprise me as much as seeing all the tests turning green when I ran its solution. For a moment, I thought using a DataTable was a hallucination. A veteran coder warned me about them the other day.

If you're still skeptical, like me when I saw that function working, here's Microsoft official docs on DataTable's Compute method. It's more Excel-like formulas inside a DataTable. The first parameter is an expression and the second one, a filter.

OK, the thing was Phind referenced 4 sources. Two of them were Medium posts talking about evaluating math expressions, but nothing inside them related to the solution it used.

And in this corner, backed by Microsoft, Copiiiiilot:

Here's Copilot's solution:

using System;
using System.Data;

class Program
{
    static void Main()
    {
        Console.WriteLine(Evaluate("one+one")); // Output: 2
        Console.WriteLine(Evaluate("two-two-one-two")); // Output: -3
        Console.WriteLine(Evaluate("one+two-one-one+two+one")); // Output: 4
    }

    public static int Evaluate(string expression)
    {
        // Replace "one" and "two" with their numeric equivalents
        expression = expression.Replace("one", "1").Replace("two", "2");

        // Use DataTable to evaluate the expression
        DataTable table = new DataTable();
        return Convert.ToInt32(table.Compute(expression, string.Empty));
    }
}
Enter fullscreen mode Exit fullscreen mode

OK, I used Copilot on a browser, not inside my Visual Studio or Visual Studio Code. Same trick with DataTable. I wasn't impressed the second time.

Not even in my dreams, (and I don't dream about code) I would have thought about using DataTable as the trick under my sleeve here, especially with a clock-ticking on a platform like LeetCode or with a hiring manager looking at my screen.

Did AI kill the tech interview? I'd say yes, at least for data structure and algorithm questions. OK, I only tried it with one coding exercise, but that's still a yes.


Join my email list and get a short, 2-minute email with 4 curated links about programming and software engineering delivered to your inbox every Friday.

Top comments (15)

Collapse
 
goodevilgenius profile image
Dan Jones

I don't know anything about DataTable (not a C# developer), but my first question would be what the performance difference is between the two (your solution, and Phind's).

My second question would be about the size of the compiled program.

An "elegant" solution is not always the best.

Collapse
 
canro91 profile image
Cesar Aguirre

Those are interesting questions Dan. I didn't take the time to run benchmarks, I was surprised enough that I rushed to write about it :)

An "elegant" solution is not always the best.

Ohhh! Good point!

Collapse
 
capture profile image
Capture

Awesome

Collapse
 
baltasarq profile image
Baltasar García Perez-Schofield • Edited

This is my solution in JavaScript (before reading the rest of your post):

/** Evaluates an expression with variables
  * "one" == 1 and
  * "two" == 2.
  */
function evaluate0(expr)
{
  expr = expr.replace( /one/g, "1" )
              .replace( /two/g, "2" );

  return eval( expr );
}
Enter fullscreen mode Exit fullscreen mode

This is what I call a "quick and dirty solution". I'd have felt guilty because of using eval() (yeah, never a good idea), but then I'd have remembered that this was an exercise for a stupid interview, and I would have handed in as it was.

Only later I realized that I was supposed to use C#. In my case, I'd have used Roslyn in order to evaluate the solution, I suppose.

Collapse
 
canro91 profile image
Cesar Aguirre

That gets all the tests turn green...With a clock ticking, my quick and dirt solution was a string processing solution, parsing each character, it only made me waste time. :\

Collapse
 
baltasarq profile image
Baltasar García Perez-Schofield • Edited

Okay, the preprocessor solution (I could not resist, I guess):

        const EXPR = "one+two-one-one+two+one";

        const OPR = {
          "ADD": 1110,
          "SUB": 1111 
        };


        function isInt(x)
        {
          return x[ 0 ] >= '0' && x[ 0 ] <= '9';
        }


        function rawEval(op1, opr, op2)
        {
          let toret = parseInt( op1, 10 );
          op2 = parseInt( op2, 10 );

          if ( opr == OPR.SUB ) {
            toret -= op2;
          }
          else
          if ( opr == OPR.ADD ) {
            toret += op2;
          } else {
            throw new TypeError( "unexpected operation: " + opr );
          }

          return toret;
        }


        function evaluate(expr)
        {
          expr = expr.trim()
                      .replace( /one/g, "1" )
                      .replace( /two/g, "2" );

          let pos = 0;
          let toret = 0;
          let lastOpr = OPR.ADD;

          while( pos < expr.length ) {
            if ( isInt( expr[ pos ] )) {
              toret = rawEval( toret, lastOpr, expr[ pos ] );
              ++pos;
            }
            else
            if ( expr[ pos ] === '+' ) {
              lastOpr = OPR.ADD;
              ++pos;
            }
            else
            if ( expr[ pos ] === '-' ) {
              lastOpr = OPR.SUB;
              ++pos;
            } else {
              throw new TypeError( "unexpected: " + expr[ pos ] );
            }
          }

          return toret;
        }

        evaluate( EXPR );
Enter fullscreen mode Exit fullscreen mode

It's again JavaScript, but used more like Java, so I think it is recognizable.

As you can see, I would anyways replace "one" with 1 and "two" with 2... it simplifies things.

I use an isInt() function. This is an entrypoint for a hypotetical solution with numbers of any width (not only 1-digit ints). This nearly does not add complexity to the solution, and I think it makes it more robust. Just a little bit of overengeering tradeoff

I also don't like that char 0 being the starting status, so I coded with the last operation being add as default, and the value to return 0.

My two cents.

Collapse
 
baltasarq profile image
Baltasar García Perez-Schofield

...I also feel a little bit hurt about my post about AI, or my reputation, were not enough for you, and you had to ask a veteran...

[This is a joke. Yep, I'm that stupid. :-P ]

Collapse
 
dotallio profile image
Dotallio

Wow, I never would've reached for DataTable.Compute in an interview either. Do you think these kinds of shortcuts actually make the hiring process any better, or just weirder?

Collapse
 
canro91 profile image
Cesar Aguirre

It makes the hiring process weirder for both sides. From candidates using AI to lip sync to companies expecting fully functional solutions for take-home exercises.

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

Man, learning that DataTable trick after all these years kinda blew my mind - always stuff hiding in plain sight.

Collapse
 
canro91 profile image
Cesar Aguirre

For a moment, I thought it was a hallucination. I had to double check and run the code on my side just to convinced me it was right. Blew my mind too, so I had to write this post.

Collapse
 
parag_nandy_roy profile image
Parag Nandy Roy

Fascinating how both models landed on the same creative solution...

Collapse
 
canro91 profile image
Cesar Aguirre

yeah, it wasn't that surprising the second time I saw the DataTable trick :\

Collapse
 
flatingofamily profile image
Andrii Novichkov

AI dev tools are getting wild lately - sometimes they impress, sometimes they hallucinate like it’s jazz night. Curious to hear what surprised you most!

Collapse
 
canro91 profile image
Cesar Aguirre

The solution with the DataTable really surprised me. (That was what mad me write this whole post.) That's something we wouldn't come up with unless we had devoured the official docs or ran into an issue.