• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Efficient Palindrome

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic