2

I have solved the problem using this algorithm and have calculated that the total number of recursive calls is n/2 since we always pass a substring of the original string with 2 less characters. The runtime complexity of the slice() function is O(n). If there are n/2 function calls and in each one we call the slice function taking O(n) time, wouldn't the time complexity of this algorithm be O(n/2 * n) or O(n^2)?

function checkIfPalindrome(string) {
  const leftMost = string[0],
    rightMost = string[string.length - 1];
  //base case 1: if length of original string is an odd number, then this base case applies
  if (string.length === 1) return true;

  //base case 2: if length of original string is an even number, then this base case applies
  if (string.length === 2) {
    if (leftMost === rightMost) return true;
    else return false;
  }

  //check if leftmost char === rightmost character
  //if true, continue recursive process, else return false;
  if (leftMost !== rightMost) return false;

  return checkIfPalindrome(string.slice(1, string.length - 1));
}

I ran the code through chatGPT and it said the Big O notation for this is O(n) but I am taking that with a grain of salt.

Phil
  • 157,677
  • 23
  • 242
  • 245
WilliamG
  • 31
  • 5
  • 3
    ChatGPT is not a programmer. And your code is not `O(n)` because of those `.slice()` calls; you're making new strings and those are linear operations. – Pointy Mar 17 '23 at 03:25
  • 1
    There's no reason for a recursive algorithm, just iterate with index variables from both ends of the string until the indexes cross. – Pointy Mar 17 '23 at 03:25
  • 2
    I know there are more efficient ways to solve this problem. it's just that I was asked to solve the problem using recursion and this is what I came up with. This post was made with the intention of improving at analyzing algorithms and efficiency. – WilliamG Mar 17 '23 at 03:28
  • Come on guys. OP is just asking what the time complexity of the simple algorithm in the question is. The argument that `slice()` is `O(n)` is already taken into account into the question. The fact that recursion is not the best approach is irrelevant, as well as the fact that ChatGPT provided a wrong answer. – Robby Cornelissen Mar 17 '23 at 03:31
  • 1
    @Megapteranovaeangliae that is simply not correct. The `.slice()` calls don't have any idea what the algorithm is doing, and a call to `.slice()` is clearly linear (`O(n)`). Performing linear operations as part of a recursive function like that means the overall "Big O" is not linear. If it's super-linear, it's super-linear; "taking into account" the `.slice()` calls does not make any sense. – Pointy Mar 17 '23 at 03:33
  • 3
    As far as I'm aware, in big-O notation, `O(n/2 * n)` and `O(n^2)` are equivalent as the constant factor is dropped, so the answer is `O(n^2)`. – Robby Cornelissen Mar 17 '23 at 03:35
  • @RobbyCornelissen what is the point of this website? Somebody's asking a question about a foundational topic in computer programming, so a pedantic answer is clearly called-for. *edit* that's true except when the sub-operation "slices" the original value by a significant factor. That's how you get `n log(n)` algorithms. – Pointy Mar 17 '23 at 03:35

1 Answers1

5

I have solved the problem using this algorithm and have calculated that the total number of recursive calls is n/2 since we always pass a substring of the original string with 2 less characters. The runtime complexity of the slice() function is O(n). If there are n/2 function calls and in each one we call the slice function taking O(n) time, wouldn't the time complexity of this algorithm be O(n/2 * n) or O(n^2)?

Yes, your reasoning and conclusion are correct. The time complexity is O(n2) = O(n2/2) = O(n/2 * n). The first of those is the conventional expression and probably the one that your grader would prefer, but all are technically correct.

I ran the code through chatGPT and it said the Big O notation for this is O(n)

Which tells you that you would be better off not consulting ChatGPT on your homework questions. It only confused you this time, but if you had come to the wrong conclusion on your own then it might have affirmed your incorrect answer. ChatGPT is trained to produce responses that will be of about the right form, but it isn't particularly good at getting answers that are correct, especially for questions that hinge on details.


From ChatGPT itself:

You

How reliable are ChatGPT's answers?

ChatGPT

As an AI Language Model, I strive to provide the most accurate and relevant information to the best of my abilities. However, ChatGPT's answers may not always be 100% accurate or appropriate for every situation or person. It is essential to consider the sources of the information I provide and use your judgment when making decisions based on my responses. In critical situations or cases where accuracy is crucial, it is best to consult with a qualified expert in the particular field.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157