• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Efficient Palindrome  RSS feed

 
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?
 
Marshal
Posts: 64494
225
  • 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
 
Bartender
Posts: 1845
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: 64494
225
  • 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.
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!