Not disclosed Interview Question for Tech Leads


Country: India




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

In C, integers are 2 to 4 bytes long. ASCII characters are 1 byte long. We can pack 4 bytes into one 4bytes integer.

- NoOne December 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Should be straight forward. One character takes 8 bytes. An integer is 4 bytes so it can hold maximum of 4 characters. That means first 8 bytes of the integer will store first char, the next bytes of the integer will store the next char and so on. So you after storing every char you will have right shift the integer with 8 bytes and perform an OR operation to store the next character so you do not corrupt the previously stored character. Something like following

int num = 0;
    if ( str.length() <= 4 ) {
        for ( int i = 0; i < str.size(); i++ ) {
            int char_i = (int)str[i];
            char_i = char_i << (i*8);
            num = num | char_i;
        }
    }

Now figure out how you can read characters from this integer.

- Farrukh Arshad December 27, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

char takes 8 bytes or 8 bits(1 byte)

- sareen.mrajat December 29, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Should be straight forward. One character takes 8 bytes. An integer is 4 bytes so it can hold maximum of 4 characters. That means first 8 bytes of the integer will store first char, the next bytes of the integer will store the next char and so on. So you after storing every char you will have right shift the integer with 8 bytes and perform an OR operation to store the next character so you do not corrupt the previously stored character. Something like following

int num = 0;
    if ( str.length() <= 4 ) {
        for ( int i = 0; i < str.size(); i++ ) {
            int char_i = (int)str[i];
            char_i = char_i << (i*8);
            num = num | char_i;
        }
    }

Now figure out how you can read characters from this integer.

- Farrukh Arshad December 27, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actually an int is not guaranteed to be 4 bytes on all platforms, so we can ise stdint.h's uint32_t to get sure 4bytes width

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

uint32_t str_to_int(char *in) {
    if (!in)
        return 0;
    uint32_t ret = 0;
    int i = 0;
    while (i < 4) {
        ret |= (in[i] << 8 * i++);
    }
    return ret;
}

void int_to_str(uint32_t in, char *out) {
    if (!out)
        return;
    int i = 0;
    while (i < 4) {
        out[i++] = in >> 8 * i;
    }
}

int main(int argc, char **argv) {
    char *in = "ABxy";

    uint32_t int_out = str_to_int(in);
    printf("%x\n", int_out);

    char *out = malloc(4);
    int_to_str(int_out, out);
    printf("%s\n", out);

    free(out);
    return 0;
}

- happysingshappy December 29, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

- Neo April 15, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Are your characters only alphabets?

- 123ayanjit April 15, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int num = 0;
if ( str.length() <= 4 ) {
for ( int i = 0; i < str.size(); i++ ) {
int char_i = (int)str[i];
char_i = char_i << (i*8);
num = num | char_i;
}
}

- pragatichandrekar September 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

union data
{
    int x;
  struct chardata pack(1)
  {
     char a;
     char b;
     char c;
     char d
  } chdata;
} undata ;

 undata.chdata.a = 'A';
 undata.chdata.b= 'B';
 undata.chdata.c = 'C';
 undata.chdata.d= 'D';
 //print the data of 
//undata.x

- Anonymous December 03, 2019 | 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