About Me

Monday, December 9, 2013

Given a stream of characters, find the first non-repeating character from stream. You need to tell the first non-repeating character in O(1) time at any moment.


3 comments:

  1. y u copy and give no creds to original...seriously dude stop being copycat this is a coding blog so maintain some level of authenticity ...

    ReplyDelete
  2. Please check the post again , this blog is maintained by many people , i have just initiated it . Thanks for comment .

    ReplyDelete
  3. /*

    Given a (decimal - e.g. 21.125) number N that is passed in as a
    string, print the binary
    representation(as a string).

    If the number cannot be represented accurately in binary, print "ERROR"
    Take care of all edge cases. [Output only 4 significant binary digits
    after decimal]
    */

    #include

    int printBinaryStr(char *decimal);
    int validate_Print(char* str);
    int main()
    {
    int i;
    char decimal[5][20] ={"8.5","4.6","8","65.23.2","0.1234"};
    for(i=0;i<5;i++)
    {
    printBinaryStr(decimal[i]);
    }

    return 0;
    }

    int printBinaryStr(char *decimal)
    {
    float number;
    if(!validate_Print(decimal))
    printf("ERROR\n");
    return 0;
    }

    int validate_Print(char* str)
    {
    unsigned int intPart=0;
    int bin_len=0;
    float floatPart =0;
    int precision = 1;
    char binary[50];
    int front,last,temp; // for string reversal
    while(*str !='\0')
    {
    if(*str >='0' &&* str<='9')
    intPart = intPart* 10 +( *str -'0');
    else if(*str =='.')
    {
    str++;
    break;
    }
    else
    return 0;
    str++;
    }

    while(*str !='\0')
    {
    if(*str >='0' &&* str<='9')
    {
    if(precision<=4)
    {
    switch(precision)
    {
    case 1:
    floatPart = floatPart + (float)( *str -'0')/10;
    break;
    case 2:
    floatPart = floatPart + (float)( *str -'0')/100;
    break;
    case 3:
    floatPart = floatPart + (float)( *str -'0')/1000;
    break;
    case 4:
    floatPart = floatPart + (float)( *str -'0')/10000;
    break;
    }
    precision++;
    }
    }
    else
    return 0;
    str++;
    }

    // convert int part of number to binary and save to char array
    if(intPart==0)
    binary[bin_len++] = '0';
    else
    {
    while(intPart>0)
    {
    if(intPart&1)
    {
    binary[bin_len++] = '1';
    }
    else
    binary[bin_len++] = '0';
    intPart = intPart>>1;
    }

    // reverse string
    front =0;
    last = bin_len -1;
    while(front=1)
    {
    binary[bin_len++] = '1';
    floatPart = (float)(floatPart*2) -1;
    }
    else
    {
    binary[bin_len++] = '0';
    floatPart = (float)(floatPart*2);
    }

    if(floatPart*2 >=1)
    {
    binary[bin_len++] = '1';
    floatPart = (float)(floatPart*2) -1;
    }
    else
    {
    binary[bin_len++] = '0';
    floatPart = (float)(floatPart*2);
    }

    if(floatPart*2 >=1)
    {
    binary[bin_len++] = '1';
    floatPart = (float)(floatPart*2) -1;
    }
    else
    {
    binary[bin_len++] = '0';
    floatPart = (float)(floatPart*2);
    }

    if(floatPart*2 >=1)
    {
    binary[bin_len++] = '1';
    floatPart = (float)(floatPart*2) -1;
    }
    else
    {
    binary[bin_len++] = '0';
    floatPart = (float)(floatPart*2);
    }

    binary[bin_len] = '\0';

    //print binary
    printf("%s\n",binary);
    return 1;

    }

    ReplyDelete

Hi thanks , we will get back to you shortly !!!