• Post Reply Bookmark Topic Watch Topic
  • New Topic

Efficient Palindrome  RSS feed

 
peer camel
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was asked in one of the interview on how to Implement a method that receives List of Objects as parameter and returns true if List is palindrome .
Examples of inputs and outputs:
{1,2,3,4,3,2,1} -> true
{a,b,c,g,c,b,a} -> true
{cat, dog, bird, cat, dog} -> false

Please do your best and provide a solution with the best possible time and space complexity i.e. the fastest and most memory efficient solution. In the comments for the method please write your estimate of the space and time complexity of your implementation using big O notation.
The more JUnit tests, the better.


Can you anyone find what is wrong in the above code? Of course they want to pass me on the interview. I thought this is the best I could do. Can anyone provide any improvement?
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That is not the way this website works. You tell us what is wrong with your code, and we shall see whether we agree
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your implementation of the algorithm looks fine to me. Time complexity - O[n] I'm pretty sure is the best you can do.
What about space complexity?
Being pedantic, your instructions were: Implement a method that receives List of Objects as parameter and returns true if List is palindrome .

You wrote a method:

The parameter there is an array of objects, not a List.


Would be closer, and require a different implementation.
And it opens up a whole lot more things to consider. How does using a List instead of an array affect the space and time complexities?
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see several stylistic problems with that code.
I would also dispute the error message for a 1‑element array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!