DEV Community

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

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

Cesar Aguirre on June 02, 2025

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 w...
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
 
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.

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 :\