I was asked at an interview, the efficient way to solve a problem checking for pallindrome.
Now i can do two things:
- starting from i = 0 to i = n/2 and comparing ith and n-ith character to be equal.
- I can use recursion to check if first and last are same and the rest of the string is a pallindrome.
The second is recursive. My question is what is the difference in the space complexity of an algorithm's recursive and non-recursive versions?
public class TailTest { public static void main(String[] args) { System.out.println(TailTest.iterate(100000)); } public static long iterate(int n){ if(n==1) return 1; return iterate(n-1); } }– Sudhakar Jul 03 '13 at 14:23