Adobe Interview Question for Software Engineer / Developers






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

Make an array of 26 ints "int hash[26]".
'start' and 'end' mark the current substring processed.
'mstart' and 'mend' mark the longest found substring till now

put -1 in all buckets of hash.
loop for each str[i] in the string
    if ( hash[str[i]] == -1 ) {
         end++ ;
         hash[str[i]] = i ;
    }
    else {
         int p = hash[str[i]] ; // Position of conflict. Move your start index ahead
         while ( start < p ) {
              hash[str[start]] = -1  // Clearing the hash for removed characters.
              start++ ;
         }
         end = i ;
    }
    if ( end - start > mend - mstart ) {
         mend = end 
         mstart = start    // Update if larger string found
    }
}

After this, your substring will be held within the indices 'mstart' and 'mend'
PS: This is an algo i came up with not the perfect code. Please dont complain about the syntax.

- Gaurav July 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As far as I understood question (please correct me if I'm wrong), if we have string which consists of characters s[0],s[1],...,s[n-1] the goal is to find such maximum length substring s[i],s[i+1],...,s[j] that for each k!=i there is no equal substring s[k],s[k+1],...,s[k+j-i].
If so, the only case when s[0],s[1],...,s[n-2] cannot be such maximum unique substring is when all symbols in original string are equal (because in suh case it should be equal to s[1],s[2],...,s[n-2]). If all symbols are equal, than largest unique substring is 1 symbol.

- Zerg July 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't the WHOLE string is a the Largest unique substring of the string?

- anon July 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good one

- good one July 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Nopes, consider abcdabcd
Longest unique subtring is abcd here, not the full string.

- Saurabh January 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

saurabh abcdabcd is a unique string... "abcdabcd" is also a unique sbstrting.

- pb February 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{
#define ALPHABET_SIZE 26
string longestUniqueSubstring(string& str)
{
int i(0), j(0), max_len(1), max_idx(0);
vector<bool> visited(ALPHABET_SIZE);
for (int i=0; i < str.size(); i++)
{
while (visit[str[i]] && i != j)
visit[str[j]] = 0;
visit[str[i]] = 1;
if (i-j+1 > max_len) { max_len = i-j+1; max_idx = i ; }
}
return str.substr(max_idx, max_len);
}
}}

- Anonymous July 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>

main(int argc, char **argv) {
        char *s;
        unsigned long bits[4] = {0,0,0,0};
        unsigned long set = 0;
        unsigned char i,c;
        register int curr;
        int lus_candidate, lus;
        int lus_candidate_len, lus_len;
        if (argc == 2) {
                s = argv[1];
        } else {
                printf("Nothing to do.\n");
                exit(0);
        }
        lus_candidate = lus = curr = 0;
        lus_candidate_len = lus_len = 0;
        printf("string: %s\n",s);
        for(curr=0; curr<strlen(s); curr++) {
                c = (unsigned char)s[curr];
                i = c >> 5;
                set = 0x01<<(c & 0x1F);
                if (bits[i] & set) {
                        do {
                                c = (unsigned char)s[lus_candidate];
                                bits[c >> 5] ^= 0x01<<(c & 0x1F);
                                lus_candidate_len--;
                        } while(s[lus_candidate++] != s[curr]);
                }
                bits[i] |= set;
                if (++lus_candidate_len >= lus_len) {
                        lus = lus_candidate;
                        lus_len = lus_candidate_len;
                }
        }
        if (lus_len) {
                printf("lus   %s%*s%.*s\n\"%.*s\" at offset %d with length=%d.\n",
                        lus==0 ? ":":": ",lus," ",lus_len,&s[lus],
                        lus_len,&s[lus],lus,(int)lus_len);
        } else {
                printf("Nothing found.\n");
        }
}

- commodius_vicus March 05, 2015 | 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