GeeksforGeeks » Interview Questions

Google Interview Question for Software Engineer/Developer (Fresher) about Algorithms, Data Structure

(12 posts)
[closed]
  • Started 10 months ago by ramya
  • Latest reply from geeksforgeeks
  1. ramya
    guest
    Posted 10 months ago #

    Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.

    hope example is correct??

  2. rajcools
    Member
    Posted 10 months ago #

    keep a bit in a bit vector for different characters and an array for storing latest positon of character. set every bit to one for character u encounter. if bit is already one it is repetitive. start counting again from this position

    for example
    abcabcbb
    bit vector start 00000000[showing 8 bits only its length will be number of bits equal to number of characters ]
    positon array 00000000[here 0 represents 32-bit we will need number of integer equal to number of characters]
    encountering a
    bit vector 00000001 position array 00000001count=1, starting index = 1 maxlength = 2
    b: 00000011 position array 00000021count =2 maxlength =2
    c:00000111 position array 00000321 count =3 maxlength =3
    a: we find a repetion array 00000324 count =3[now count is 3 for bca as length of this substring is also 3] maxlength=3

    and so on...

    the example that op has taken is ambigous as answer is not only abc its bca and also cab :)

  3. ramya
    guest
    Posted 10 months ago #

    @rajcools can u right the code for this please ..??

  4. Andy
    Member
    Posted 10 months ago #

    @ramya: this is very easy to code, I will suggest you to give it a try..

  5. rajcools
    Member
    Posted 10 months ago #

    @andy yes it is very easy to code.
    ramya give it a try

  6. ramya
    guest
    Posted 10 months ago #

    @rajcools actually i confused how u are setting position things are messing up how u r using position array . i know its easy to code as ur algo is differ from my approach so i suggest you to put ur code using algo suggested then i will also show u my algo & code :)

    waiting for reply

  7. ramya
    guest
    Posted 10 months ago #

    @rajcools actually i confused how u are setting position things are messing up how u r using position array . i know its easy to code as ur algo is differ from my approach so i suggest you to put ur code using algo suggested then i will also show u my algo & code :)

    waiting for reply

  8. rajcools
    Member
    Posted 10 months ago #

    @ramya sorry i was very busy
    here is the code based on my algo... to keep simple i have made no changes to what i have explained above
    it will work only for small alphabets a-z no other character. To use any other character just make the simple changes to the array size and one or more cosmetic changes it will work but idea will be the same

    #include<stdio.h>
    #include<stdlib.h>
    //utility function
    //setbit function using 26 bits out  of 32 bits in an integer
    void setbit(int *bitvector ,int bit)
    {
         *bitvector = *bitvector |(1<<bit);
    }
    int getbit(int bitvector, int bit)
    {
        return (bitvector&(1<<bit))>>bit;
    }
    int main()
    {
        char *str = "aabcdefa";
        int bitvector = 0;
        int positionarray[26] = {0};
        char *tempstr = str;
        int count =0;
        int max =0;
        while(*tempstr)
        {
                  if(getbit(bitvector,(*tempstr)-'a'))
                  {
    
                           count  = (tempstr-str) -(positionarray[*tempstr -'a']);
                  }
                  else
                  {
                      count++;
                      setbit(&bitvector,*tempstr-'a');
                  }
                  positionarray[*tempstr -'a'] = tempstr-str;
                  if(max<count)
                  max = count;
                  tempstr++;
        }
        printf("%d\n",max);
        getchar();
    }
  9. questioner
    guest
    Posted 10 months ago #

    @rajcools Does your code work for "baaaaaaaaaab"?

  10. rajcools
    Member
    Posted 10 months ago #

    now it does... code modified

    #include<stdio.h>
    #include<stdlib.h>
    //utility function
    //setbit function using 26 bits out  of 32 bits in an integer
    void setbit(int *bitvector ,int bit)
    {
         *bitvector = *bitvector |(1<<bit);
    }
    int getbit(int bitvector, int bit)
    {
        return (bitvector&(1<<bit))>>bit;
    }
    int main()
    {
        char *str = "abcdef";
        int bitvector = 0;
        int positionarray[26] = {0};
        char *tempstr = str;
        int count =0;
        int max =0;
        char repetitionchar ='';
        int repetitioncount  =0;
        while(*tempstr)
        {
                  if(getbit(bitvector,(*tempstr)-'a'))
                  {
                                                       if(repetitionchar == *tempstr)
                                                       {
                                                       repetitioncount++;
                                                       count  = (tempstr-str) -(positionarray[*tempstr -'a']);
                                                       }
                                                       else
                                                       {
                                                       count  = (tempstr-str) -(positionarray[repetitionchar -'a'])-repetitioncount;
                                                      repetitionchar =*tempstr;
                                                        }       
    
                  }
                  else
                  {
                      count++;
                      setbit(&bitvector,*tempstr-'a');
                  }
                  positionarray[*tempstr -'a'] = tempstr-str;
                  if(max<count)
                  max = count;
                  tempstr++;
        }
        printf("%d\n",max);
        getchar();
    }
  11. Bala
    guest
    Posted 9 months ago #

    Your code fails for this input "abcadefg"

  12. GeeksforGeeks
    Key Master
    Posted 5 months ago #

    This is published. Please see http://www.geeksforgeeks.org/archives/15859


Topic Closed

This topic has been closed to new replies.

RSS feed for this topic