Fortinet Interview Question for Software Engineer / Developers


Country: Canada
Interview Type: Written Test




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

void Encode( std::string& outputString, unsigned char input[], int inputSize )
{
    const char base64_map[] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz"
    "0123456789+/";

    outputString = "";

    int encode = 0;
    int i = 0;
    const int encode4GroupMask = 0x003F;
    const int encode3GroupMask = 0x003F << 6;
    const int encode2GroupMask = 0x003F << 12;
    const int encode1GroupMask = 0x003F << 18;
    int encodeSize = inputSize;
    while( encodeSize >= 3 )
    {
        encode = 0;
        encode = input[ i ] << 16 | input[ i + 1 ] << 8 | input[ i + 2 ];
        int index1 = ( encode & encode1GroupMask ) >> 18;
        int index2 = ( encode & encode2GroupMask ) >> 12;
        int index3 = ( encode & encode3GroupMask ) >> 6;
        int index4 = ( encode & encode4GroupMask );

        printf( "Encode 0x%X Index1 %d Index2 %d Index3 %d Index4 %d\n", encode, index1, index2, index3, index4 );
        outputString += base64_map[ index1 ];
        outputString += base64_map[ index2 ];
        outputString += base64_map[ index3 ];
        outputString += base64_map[ index4 ];

        encodeSize -= 3;
        i += 3;
    }

    if( encodeSize > 0 )
    {
        encode = 0;
        if( encodeSize == 1 )
        {
            encode  = input[ i ] << 16;
            int index1 = ( encode & encode1GroupMask ) >> 18;
            int index2 = ( encode & encode2GroupMask ) >> 12;
            printf( "Encode 0x%X Index1 %d Index2 %d\n", encode, index1, index2 );
            outputString += base64_map[ index1 ];
            outputString += base64_map[ index2 ];
            outputString += "=";
            outputString += "=";
        }
        else
{
            if( encodeSize == 2 )
            {
                encode  = input[ i ] << 16 | input[ i + 1 ] << 8;
                int index1 = ( encode & encode1GroupMask ) >> 18;
                int index2 = ( encode & encode2GroupMask ) >> 12;
                int index3 = ( encode & encode3GroupMask ) >> 6;
                printf( "Encode 0x%X Index1 %d Index2 %d Index3 %d\n", encode, index1, index2, index3 );
                outputString += base64_map[ index1 ];
                outputString += base64_map[ index2 ];
                outputString += base64_map[ index3 ];
                outputString += "=";
            }
        }
    }
}

int main()
{
    unsigned char input[] = "This is a tes";
    int inputSize = strlen( (char *)input );
    std::string output;
    Encode( output, input, inputSize );
    printf( "Output %s\n", output.c_str() );
    return( 0 );
}

- TheNutto June 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

They don't have this question on their test anymore, but here is my simple solution:

const char base64_map[] =
         		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        		"abcdefghijklmnopqrstuvwxyz"
				"0123456789+/";

size_t b64_length(size_t len)
{
	if (( (len * 8) % 24) != 0 )
		return (len * 8 + (24 - ((len * 8) % 24))) / 6;
	else
		return (len * 8 / 6);
}

void b64_encode(const void * src, size_t len, void * dst)
{
	if (!src || !dst || len <= 0) 
	{	
		printf("empty ");
		return;
	}

	unsigned int * srccode = (unsigned int *)src;

	char * dstcode = (char *)dst + b64_length(len); // point to the end of *dst string
	strcpy(dstcode, ""); //set *dst string end with null char \0

	size_t pad = b64_length(len) - (3 - len % 3);
	size_t i = 0;
	for (i = 0; i < b64_length(len); i++)
	{
		// encode a byte
		unsigned int nextbyte = *srccode & 0x3f;
		char codechar;
		if ( i < pad ) codechar = base64_map[nextbyte];
		else codechar = '='; // encode padded byte
		const char nextchar = codechar;

		--dstcode;
		memset(dstcode, nextchar, sizeof(char));

		// process 6 bits in an iteration
		*srccode = *srccode >> 6;
	}
}

int main()
{
	unsigned int inputcode = 0x000045f2;
	const unsigned int * src = &inputcode;
	size_t len = 4; // src len
	printf("%zd\n", b64_length(len));

	char * dst = (char *)malloc(b64_length(len) + 1);
	b64_encode(src, len, dst);
	printf("%s", dst);
	free(dst);

	return (0);
}

- txf October 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

int convertString2hex(string s)
{
int length=s.length();
int fullstringinhex=0;
int stringinhex=0;

for (int i=0;i<length;i++)
{if (s[i]=='0')
stringinhex=0x0;
else if (s[i]=='1')
stringinhex=0x1;
else if (s[i]=='2')
stringinhex=0x2;
else if (s[i]=='3')
stringinhex=0x3;
else if (s[i]=='4')
stringinhex=0x4;
else if (s[i]=='5')
stringinhex=0x5;
else if (s[i]=='6')
stringinhex=0x6;
else if (s[i]=='7')
stringinhex=0x7;
else if (s[i]=='8')
stringinhex=0x8;
else if (s[i]=='9')
stringinhex=0x9;
else if ((s[i]=='a')||(s[i]=='A'))
stringinhex=0xa;
else if ((s[i]=='b')||(s[i]=='B'))
stringinhex=0xb;
else if ((s[i]=='c')||(s[i]=='C'))
stringinhex=0xc;
else if ((s[i]=='d')||(s[i]=='D'))
stringinhex=0xd;
else if ((s[i]=='e')||(s[i]=='E'))
stringinhex=0xe;
else if ((s[i]=='f')||(s[i]=='F'))
{stringinhex=0xf;cout<<"here"<<endl;}
else
{cout<<"Not a valid string"<<endl;}

fullstringinhex=(fullstringinhex|stringinhex)<<4;
}
return fullstringinhex>>4;}


void Encode( string str)
{
int length=str.length();
int input=convertString2hex(str);

const char base64_map[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";


//group1|group2|group3|group4
const int encode4GroupMask = 0x00003F;
const int encode3GroupMask = 0x000FC0;
const int encode2GroupMask = 0x03F000;
const int encode1GroupMask = 0xFC0000;

if ((length==6)||(length==5))
{int index4=(encode4GroupMask&input);
int index3=(encode3GroupMask&input) >> 6;
int index2=(encode2GroupMask&input) >> 12;
int index1=(encode1GroupMask&input) >> 18;

cout<<index4<<" "<<index3<<" "<<index2<<" "<<index1<<endl;
cout<<"Output "<<base64_map[index1]<<base64_map[index2]<<base64_map[index3]<<base64_map[index4]<<endl;
}

if (length==4)
{int index4=(encode4GroupMask&input);
int index3=(encode3GroupMask&input) >> 6;
int index2=(encode2GroupMask&input) >> 12;
//int index1=(encode1GroupMask&input) >> 18;

cout<<index4<<" "<<index3<<" "<<index2<<endl;
cout<<"Output "<<base64_map[index2]<<base64_map[index3]<<base64_map[index4]<<endl;
}

if ((length==3)||(length==2))
{int index4=(encode4GroupMask&input);
int index3=(encode3GroupMask&input) >> 6;
//int index2=(encode2GroupMask&input) >> 12;
//int index1=(encode1GroupMask&input) >> 18;

cout<<index4<<" "<<index3<<endl;
cout<<"Output "<<base64_map[index3]<<base64_map[index4]<<endl;
}
if (length==1)
{int index4=(encode4GroupMask&input);
//int index3=(encode3GroupMask&input) >> 6;
//int index2=(encode2GroupMask&input) >> 12;
//int index1=(encode1GroupMask&input) >> 18;
cout<<index4<<endl;
cout<<"Output "<<base64_map[index4]<<endl;
}

}

int main()
{
string str="000";

Encode( str );

return 0 ;
}

- Anonymous February 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

int convertString2hex(string s)
{
int length=s.length();
int fullstringinhex=0;
int stringinhex=0;

for (int i=0;i<length;i++)
{if (s[i]=='0')
stringinhex=0x0;
else if (s[i]=='1')
stringinhex=0x1;
else if (s[i]=='2')
stringinhex=0x2;
else if (s[i]=='3')
stringinhex=0x3;
else if (s[i]=='4')
stringinhex=0x4;
else if (s[i]=='5')
stringinhex=0x5;
else if (s[i]=='6')
stringinhex=0x6;
else if (s[i]=='7')
stringinhex=0x7;
else if (s[i]=='8')
stringinhex=0x8;
else if (s[i]=='9')
stringinhex=0x9;
else if ((s[i]=='a')||(s[i]=='A'))
stringinhex=0xa;
else if ((s[i]=='b')||(s[i]=='B'))
stringinhex=0xb;
else if ((s[i]=='c')||(s[i]=='C'))
stringinhex=0xc;
else if ((s[i]=='d')||(s[i]=='D'))
stringinhex=0xd;
else if ((s[i]=='e')||(s[i]=='E'))
stringinhex=0xe;
else if ((s[i]=='f')||(s[i]=='F'))
{stringinhex=0xf;cout<<"here"<<endl;}
else
{cout<<"Not a valid string"<<endl;}

fullstringinhex=(fullstringinhex|stringinhex)<<4;
}
return fullstringinhex>>4;}


void Encode( string str)
{
int length=str.length();
int input=convertString2hex(str);

const char base64_map[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";


//group1|group2|group3|group4
const int encode4GroupMask = 0x00003F;
const int encode3GroupMask = 0x000FC0;
const int encode2GroupMask = 0x03F000;
const int encode1GroupMask = 0xFC0000;

if ((length==6)||(length==5))
{int index4=(encode4GroupMask&input);
int index3=(encode3GroupMask&input) >> 6;
int index2=(encode2GroupMask&input) >> 12;
int index1=(encode1GroupMask&input) >> 18;

cout<<index4<<" "<<index3<<" "<<index2<<" "<<index1<<endl;
cout<<"Output "<<base64_map[index1]<<base64_map[index2]<<base64_map[index3]<<base64_map[index4]<<endl;
}

if (length==4)
{int index4=(encode4GroupMask&input);
int index3=(encode3GroupMask&input) >> 6;
int index2=(encode2GroupMask&input) >> 12;
//int index1=(encode1GroupMask&input) >> 18;

cout<<index4<<" "<<index3<<" "<<index2<<endl;
cout<<"Output "<<base64_map[index2]<<base64_map[index3]<<base64_map[index4]<<endl;
}

if ((length==3)||(length==2))
{int index4=(encode4GroupMask&input);
int index3=(encode3GroupMask&input) >> 6;
//int index2=(encode2GroupMask&input) >> 12;
//int index1=(encode1GroupMask&input) >> 18;

cout<<index4<<" "<<index3<<endl;
cout<<"Output "<<base64_map[index3]<<base64_map[index4]<<endl;
}
if (length==1)
{int index4=(encode4GroupMask&input);
//int index3=(encode3GroupMask&input) >> 6;
//int index2=(encode2GroupMask&input) >> 12;
//int index1=(encode1GroupMask&input) >> 18;
cout<<index4<<endl;
cout<<"Output "<<base64_map[index4]<<endl;
}

}

int main()
{
string str="000";

Encode( str );

return 0 ;
}

- asa February 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

int convertString2hex(string s)
{
    int length=s.length();
    int fullstringinhex=0; 
    int stringinhex=0;
    
    for (int i=0;i<length;i++)
    {if (s[i]=='0')
    stringinhex=0x0;
    else if (s[i]=='1')
    stringinhex=0x1;
    else if (s[i]=='2')
    stringinhex=0x2;
    else if (s[i]=='3')
    stringinhex=0x3;
    else if (s[i]=='4')
    stringinhex=0x4;
    else if (s[i]=='5')
    stringinhex=0x5;
    else if (s[i]=='6')
    stringinhex=0x6;
    else if (s[i]=='7')
    stringinhex=0x7;
    else if (s[i]=='8')
    stringinhex=0x8;
    else if (s[i]=='9')
    stringinhex=0x9;
    else if ((s[i]=='a')||(s[i]=='A'))
    stringinhex=0xa;
    else if ((s[i]=='b')||(s[i]=='B'))
    stringinhex=0xb;
    else if ((s[i]=='c')||(s[i]=='C'))
    stringinhex=0xc;
    else if ((s[i]=='d')||(s[i]=='D'))
    stringinhex=0xd;
    else if ((s[i]=='e')||(s[i]=='E'))
    stringinhex=0xe;
    else if ((s[i]=='f')||(s[i]=='F'))
    {stringinhex=0xf;cout<<"here"<<endl;}
    else
    {cout<<"Not a valid string"<<endl;}
    
    fullstringinhex=(fullstringinhex|stringinhex)<<4;
    }
    return fullstringinhex>>4;}


void Encode( string str)
{
    int length=str.length();
    int input=convertString2hex(str);
    
    const char base64_map[] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz"
    "0123456789+/";


    //group1|group2|group3|group4
    const int encode4GroupMask = 0x00003F;
    const int encode3GroupMask = 0x000FC0;
    const int encode2GroupMask = 0x03F000;
    const int encode1GroupMask = 0xFC0000;
    
    if ((length==6)||(length==5))
    {int index4=(encode4GroupMask&input);
    int index3=(encode3GroupMask&input) >> 6;
    int index2=(encode2GroupMask&input) >> 12;
    int index1=(encode1GroupMask&input) >> 18;

    cout<<index4<<" "<<index3<<" "<<index2<<" "<<index1<<endl;
    cout<<"Output "<<base64_map[index1]<<base64_map[index2]<<base64_map[index3]<<base64_map[index4]<<endl;
    }
    
    if (length==4)
    {int index4=(encode4GroupMask&input);
    int index3=(encode3GroupMask&input) >> 6;
    int index2=(encode2GroupMask&input) >> 12;
    //int index1=(encode1GroupMask&input) >> 18;

    cout<<index4<<" "<<index3<<" "<<index2<<endl;
    cout<<"Output "<<base64_map[index2]<<base64_map[index3]<<base64_map[index4]<<endl;
    }
    
    if ((length==3)||(length==2))
    {int index4=(encode4GroupMask&input);
    int index3=(encode3GroupMask&input) >> 6;
    //int index2=(encode2GroupMask&input) >> 12;
    //int index1=(encode1GroupMask&input) >> 18;

    cout<<index4<<" "<<index3<<endl;
    cout<<"Output "<<base64_map[index3]<<base64_map[index4]<<endl;
    }
    if (length==1)
    {int index4=(encode4GroupMask&input);
    //int index3=(encode3GroupMask&input) >> 6;
    //int index2=(encode2GroupMask&input) >> 12;
    //int index1=(encode1GroupMask&input) >> 18;
    cout<<index4<<endl;
    cout<<"Output "<<base64_map[index4]<<endl;
    }
    
}

int main()
{
    string str="000";
    
    Encode( str );

    return 0 ;
}

- asa February 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void base64Encode(string &output, unsigned char input[], int input_size)
{
	const char base64_map[]=
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		"abcdefghijklmnopqrstuvwxyz"
		"0123456789+/";

	string output_string = "";

	int encode = 0;
	 
	int offset = 0;
	while (offset < input_size)
	{
		encode = 0;
		for (int i = 2; i >= 0; --i)
		{
			int temp;
			if (offset < input_size)
			{
				temp = input[offset] << (i * 8);
				offset++;
			}
			encode |= temp;
		}
		int index[4];

		for (int i = 0; i < 4; ++i)
		{
			int group_mask = 0x003F << (6 * (3 - i));
			index[i] = (encode * group_mask) >> (6 * (3 - i));
			output_string += base64_map[index[i]];
		}
	}
}

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

void base64Encode(string &output, unsigned char input[], int input_size)
{
	const char base64_map[]=
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		"abcdefghijklmnopqrstuvwxyz"
		"0123456789+/";

	string output_string = "";

	int encode = 0;
	 
	int offset = 0;
	while (offset < input_size)
	{
		encode = 0;
		for (int i = 2; i >= 0; --i)
		{
			int temp;
			if (offset < input_size)
			{
				temp = input[offset] << (i * 8);
				offset++;
			}
			encode |= temp;
		}
		int index[4];

		for (int i = 0; i < 4; ++i)
		{
			int group_mask = 0x003F << (6 * (3 - i));
			index[i] = (encode * group_mask) >> (6 * (3 - i));
			output_string += base64_map[index[i]];
		}
	}
}

- b.badkoubeh July 17, 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