9

In an interview, the interviewer asked me to write test cases or test scenarios to check if the input/string is a palindrome.

I told them one positive test case and another negative test cases. But employer was looking for more scenarios.

Can anyone think about more scenarios or test cases for checking/validating a palindrome?

5
  • What automated testing and unit testing have in common with this question?
    – dzieciou
    Jul 27, 2012 at 16:53
  • Well... The actual question was to write unit testing using JUnit framwork for Palindrome checker.
    – coolgokul
    Jul 27, 2012 at 16:56
  • 3
    See definition of palindrom at en.wikipedia.org/wiki/Palindrome and think about boundary cases. For instance, what happens to white spaces, and punctuation marks when compare input with its reversed form? Are they ignored or maybe even not acceptable? This is the task of a tester to explore those constrains.
    – dzieciou
    Jul 27, 2012 at 17:08
  • 2
    Re: junit tests. Do you want readers to help you write unit tests or just get general test case scenarios? If only the latter, I would remove those tags from your question.
    – dzieciou
    Jul 27, 2012 at 17:12
  • I would say both are fine.. Any written unit tests or general test scenarios are both good.
    – coolgokul
    Jul 27, 2012 at 17:57

3 Answers 3

16

Palindromes testing is very representative in terms of QA way of thinking. Moreover, writing tests dramatically increase the quality of task specification. When a QA engineer writes test cases, it well may happen that certain case is not covered in initial specs. This is a good reason for QA to come up with idea to improve those specs.

So, when you are asked for more test cases during an interview, they really want to make sure you think like a QA.

Let's see what test cases can be written. First, palindromes are based on strings, so you have to enforce common string testing:

  • Null string
  • Empty string - is it a palindrome? Probably not, but naive algorithm would return positive result.
  • Spacing and punctuation, as @dzieciou has noticed

Zero-width diacritic marks introduce several tests:

  • é (Latin small E with Acute Accent) can be represented in two ways, U+00E9 and U+0065 U+00B4, are they equal?
  • Naive reversing U+0065 U+00B4 would ruin the character, so it has to be tested as well;
  • Finally, can é and e be considered same character, in terms of palindrome?
6

See definition of palindrom and think about boundary cases.

For instance:

  • What happens to white spaces, and punctuation marks when comparing input with its reversed form? Are they ignored or maybe even not acceptable?

  • Is implementation case insensitive? Usually, both Anna and anna are considered palindroms.

This is the task of a tester to explore those constrains.

1

Don't forget even and odd length strings.

Performance test.

4
  • Can you give an example implementation of a palindrom checking function, where even length strings are, by mistake, processed differently that odd length strings. Is it possible?
    – dzieciou
    Jul 29, 2012 at 6:53
  • 1
    @dzieciou It depends on how a programming language rounds integer division results. Most round half up, so the coder would automatically use (str.length-1)/2 as upper margin for the for loop. (7-1)/2 == (6-1)/2 == 3 which is fine. The same code in C# will result (6-1)/2 == 2. Jul 29, 2012 at 8:25
  • @bytebuster My fault I was thinking of only one possible implementation (input == reverse(input)), while you probably assumed characters from a left half of a string are compared with characters on the right. From the original question it looks more like black box test. In such a case I should never assume only one possible implementation of the requirements. That's what I learned in general from this discussion.
    – dzieciou
    Jul 29, 2012 at 9:22
  • 1
    @dzieciou I agree this is very questionable whether or not pure tests of the implementation algorithm should be in QA responsibility in the real world. Maybe it is better if such test existed among unit tests (and was written by a coder). Nevertheless, you asked for a bad implementation, and here it is. :) Jul 29, 2012 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.