Adobe Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
1
of 1 vote

This question is from an ongoing competition on CodeChef.

Don't use this site to cheat.

- Miguel Oliveira December 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Miguel Oliveira, I wud have to say toughest competition questions questions are asked to be even considered for next round of job interviews only in India with non chalance.

- shiva February 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Manacher algorithm can solve this in O(n) and with suffix tree in O(NlogN)

- Ajeet December 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The question asks for distinct palindromes. I believe Manacher's algorithm won't work in this case.

I also think the best answer involves suffix trees, but I'm not sure how to construct the tree to check the distinct palindromes

- Miguel Oliveira December 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here is source code ..

package org.ajeet.algorithms.dp;

public class Manacher {
    private int[]  p;  // p[i] = length of longest palindromic substring of t, centered at i
    private String s;  // original string
    private char[] t;  // transformed string

    public Manacher(String s) {
        this.s = s;
        t = preprocess(s);
        p = new int[t.length];

        int center = 0, right = 0;
        for (int i = 1; i < t.length-1; i++) {
            int mirror = 2*center - i;

            if (right > i) p[i] = Math.min(right - i, p[mirror]);
 
            // attempt to expand palindrome centered at i
            while (t[i + (1 + p[i])] == t[i - (1 + p[i])])
                p[i]++;
 
            // if palindrome centered at i expands past right,
            // adjust center based on expanded palindrome.
            if (i + p[i] > right) {
                center = i;
                right = i + p[i];
            }
        }

    }

    // Transform s into t.
    // For example, if s = "abba", then t = "$#a#b#b#a#@"
    // the # are interleaved to avoid even/odd-length palindromes uniformly
    // $ and @ are prepended and appended to each end to avoid bounds checking
    public char[] preprocess(String s) {
        char[] t = new char[s.length()*2 + 3];
        t[0] = '$';
        t[s.length()*2 + 2] = '@';
        for (int i = 0; i < s.length(); i++) {
            t[2*i + 1] = '#';
            t[2*i + 2] = s.charAt(i);
        }
        t[s.length()*2 + 1] = '#';
        return t;
    }
 
    // longest palindromic substring
    public String longestPalindromicSubstring() {
        int length = 0;   // length of longest palindromic substring
        int center = 0;   // center of longest palindromic substring
        for (int i = 1; i < p.length-1; i++) {
            if (p[i] > length) {
                length = p[i];
                center = i;
            }
        }
        return s.substring((center - 1 - length) / 2, (center - 1 + length) / 2);
    }

    // longest palindromic substring centered at index i/2
    public String longestPalindromicSubstring(int i) {
i = i + 2;
        int length = p[i];
        int center = i;
        return s.substring((center - 1 - length) / 2, (center - 1 + length) / 2);
    }



    // test client
    public static void main(String[] args) {
        String s = "aba";
        Manacher manacher = new Manacher(s);
        //Here is longest
        System.out.println(manacher.longestPalindromicSubstring());
       //All palindromes
        for (int i = 0; i < 2*s.length(); i++)
        	System.out.println(i +  ": " + manacher.longestPalindromicSubstring(i));
         
    }
}

- Ajeet December 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your code gives {aba,a,a} as result.Had you checked it out?

- justhack4fun688 December 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry i missed the method (it utilize Manacher algo to count all distinct palindromes)..

public Map<String, Boolean> getAllPalindromicSubstring(String source){
    	Map<String, Boolean>  result = new HashMap<String, Boolean>();
    	 for (int i = 0; i < 2*s.length(); i++){
    		 	String tmp = this.longestPalindromicSubstring(i);
    	   		if(tmp != null && !tmp.equals("") && !result.containsKey(tmp)){
        			result.put(tmp, true);
        		}
    	 }
         //Manacher  algorithm skips a single character if it occures in a bigger palindrome. So need to re iterate once more to add missing single character palindromes.
    	 for (int i = 0; i < source.length(); i++){
 		 	String tmp = source.charAt(i)+"";
 	   		if(!result.containsKey(tmp)){
     			result.put(tmp, true);
     	 }
 	
    	}
    	return result;
    }

- Ajeet December 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

String s = "aba";
        Manacher manacher = new Manacher(s);
        //Here is longest
        System.out.println(manacher.longestPalindromicSubstring());
       //All palindromes
        Map<String, Boolean> palindromes = manacher.getAllPalindromicSubstring(s);

- Ajeet December 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ajeet ur code give 4 as result on "diaid" but answer should be 5 : {d,i,a,iai,diaid}

- justhack4fun688 December 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

the code does not work because it does not count smaller palindromes like 'iai' in 'diaid'. Also, doing it that way and checking with a hash table will make the algorithm run in O(N^2) - just use a string like "aaaaaa"

- Miguel Oliveira December 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Manacher's algortihm gives all the possible palindromic substrings. To get the distinct substrings, we can use a hash table and reject the duplicated ones or build a trie.

- alex December 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could u plz provide me code to implement Manacher algorithm to count distinct palindromes. I need it

- justhack4fun688 December 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

leetcode.com/2011/11/longest-palindromic-substring-part-ii.html

- alex December 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I had gone through this.But not able to modify it to count distinct palindromes.Could you do that plz?

- justhack4fun688 December 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Modify manacher to use hashing -> become N^2 instead of N for regular Manacher

Now modify hashing to use a rabin karp type optimization -> becomes N again

- leet coder December 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@leet coder could you provide a piece of code plz..

- justhack4fun688 December 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can u plz explain?

- Anonymous August 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string>

using namespace std;

bool isPalindrome(string,int,int);

int palindrome_count(string str){
        int count = 0;
        for(int i=0; i < str.size(); i++){
                for(int j=i ; j< str.size(); j++){
                        if(isPalindrome(str,i,j))
                                count++;
                }
        }
        return count;
}

bool isPalindrome(string str,int start,int end){
        while(start<=end){
                if(str[start] != str[end])
                        return false;
                else if(str[start] == str[end]){
                        start++;
                        end--;
                }
        }
        return true;
}


int main(){

        string str = "abcdcba";
        int count;
        count = palindrome_count(str);
        cout<<"\n"<<count;
}

- Anonymous January 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

lets say string given is T.

1. build a sufix tree of T$.
ex- T = aba.
then suffix = {a$, ba$, aba$}.

2. find reverse of given string T".
then ad suffix of T'' in the earlier suffix tree.
sufix of T" = {a#, ba#, aba#}

3. traverse the suffix tree. -> find the path which have ended with # and $.
these will be the pallindrome of the string.

- abhishek January 26, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More