Facebook Interview Question for Software Engineer / Developers






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

void dedupe(char *str)
{
int length = strlen(str);
int i=0,j=0;
char flag[256];
for(i=0; i<256; i++)
{
flag[i] = 0;
}
for(i=0; i<length; i++)
{
if(flag[str[i]] == 0)
{
flag[str[i]] = 1;
str[j] = str[i];
j++;
}
}
str[j] = '\0';
}

- Chuan August 19, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

//C#
 //O(n)
        static char[] removeDuplicateChars(char[] _str)
        {
            bool[] charset = new bool[256];
            for (int i = 0; i < _str.Length; i++)
            {
                int val = _str[i];
                if (charset[val]) { _str[i] = ' '; }
                charset[val] = true;
            }
            return _str;
        }

- anup.h.nair December 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

My answer in JavaScript

(function(){
    window.dedupe = function(str){
        var i, curr = 0,
            res = "",
            hash = {},
            len = str.length;

        for (i=0; i < len; i++){
            if (hash[str[i]]){
                continue;
            }
            res += str[i];
            hash[str[i]] = true;
        }
        return res;
    }
})();

- Sagivf November 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

In Java:

public static String cleanString(String arg) {

String result = "";

for (int i = 0; i < arg.length(); i++) {

char c = arg.charAt(i);

if (result.indexOf(c) == -1) {
result += c;
}
}

return result;
}

- Anonymous April 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

any ans..

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

Do a mergesort, with slight changes to the merge part: when you find two elements equal, add only of them to the list.

- Vikram February 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>

using namespace std;

#define MAX_NUM_CHAR 256

char * dedupe(char * str);

int main(void) {

char str1[5] = {'a', 'b', 'c', 'a', '\0'};
char str2[8] = {'a', 'b', 'b', 'a', 'a', 'd', 'e', '\0'};

char * str = NULL;

str = dedupe(str1);
printf("%s\n", str);
str = dedupe(str2);
printf("%s\n", str);

return 0;
}

char * dedupe(char * str)
{

unsigned int i = 0, j = 0;;
unsigned int strLen = 0;
char hasChar[MAX_NUM_CHAR];

if(str == NULL) return NULL;

// init hasChar array
for(i = 0; i < MAX_NUM_CHAR; i++) {
hasChar[i] = 0;
}

strLen = strlen(str);
for(i = 0, j = 0; i < strLen; i++) {
if(hasChar[str[i]] == 0) {
str[j++] = str[i];
hasChar[str[i]] += 1;
} else ;
}
str[j] = '\0'; // here comes memory leak? in that string has more memory than necessary

return str;
}

- freeaion March 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int length = str.size();
bool bitmap[256];
init(); //false for all 256 elements
for(int i=0; i<length; i++)
if (bitmap[str[i]]) remove(str[i]);
else
bitmap[str[i]]=true;

- beyondfalcon February 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this implementation? The algorithm is simple: maintain a dedupped string ended by pointer "end", and each time a new charactor pointer by "p" will compare to all the dedupped characters.

void RemoveDuplicates(char *str) {
if (str == NULL) return;
int len = strlen(str);
if (len <= 1) return;

char *end = str;
for (char *p = end + 1; *p; ++p) {
bool dup = false;
for (char *t = str; t <= end; ++t) {
if (*t == *p) {
dup = true;
break;
}
}
if (!dup) {
*++end= *p;
}
}
*++end= '\0';
}

- Isaac July 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void dedup(char *ptr) {

while(*ptr != '\0') {

//insert into hash table as key
*ptr = tolower(*ptr);
int index = (int)*ptr-97;
keys[index]++;
//convert char to ascii
if(keys[index] == 1) {
*result = *ptr;
result++;

}

ptr++;



}

*result = '\0';
printf("%s",result);


}

- Anonymous November 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String dedupe(String str) {
		int [] A = new int[256];
		StringBuffer sb = new StringBuffer();
		
		int len = str.length();
		for (int i=0; i<len; i++) {
			char c = str.charAt(i);
			if (A[c] == 0) {
				A[c] = 1;
				sb.append(c);
			}
		}
		
		return sb.toString();
	}

- bawet November 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
Given a string *Str of ASCII characters, write the pseudo code to remove the duplicate elements present in them.
For example, if the given string is "Potato", then, the output has to be "Pota".
Additional constraint is, the algorithm has to be in-place( no extra data structures allowed) .
Extend your algorithm to remove duplicates in the string which consisted of UNICODE characters.
*/
#include<iostream>
using namespace std;
int main()
{
char a[20],*p;
p=a;
cin >> a;
while(*p)
{
char *s = (p+1);
while(*s)
{
if(*s == *p)
{
int i=0;
while(*(s+i))
{
if(*(s+i) == *p)
i++;
*s = *(s+i);
s++;
}
*s = '\0';
}
else
s++;
}
p++;
}
cout << a << endl;
system("pause");
return 0;
}

- hi December 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
Given a string *Str of ASCII characters, write the pseudo code to remove the duplicate elements present in them.
For example, if the given string is "Potato", then, the output has to be "Pota".
Additional constraint is, the algorithm has to be in-place( no extra data structures allowed) .
Extend your algorithm to remove duplicates in the string which consisted of UNICODE characters.
*/
#include<iostream>
using namespace std;
int main()
{
char a[20],*p;
p=a;
cin >> a;
while(*p)
{
char *s = (p+1);
while(*s)
{
if(*s == *p)
{
int i=0;
while(*(s+i))
{
if(*(s+i) == *p)
i++;
*s = *(s+i);
s++;
}
*s = '\0';
}
else
s++;
}
p++;
}
cout << a << endl;
system("pause");
return 0;
}

- hi December 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#import<stdio.h>
#import<string.h>
void  moveZeros(char* a)
{
        size_t len = strlen(a);
        int index0 = 0;
        int indexN = 0;
        for(int i = 0; i < len; i++)
        {
                if(a[i] != '@')
                {
                        int temp = a[i];
                        a[i] = a[index0];
                        a[index0] = temp;
                        index0++;
                }
        }
        a[index0] = '\0';
}
void dupeWithExtraSpace(char* str)
{
	int charArray[26] = {0};
	int i, j;
	i = j = 0;
	
	for(;i < strlen(str); i++)
	{
		if(charArray[str[i] - 'a'] == 0)
		{
			str[j] = str[i];
			charArray[str[i] - 'a'] = 1;
			j++;
		}
	}
	str[j] = '\0';
}

void dupeWithNoExtraSpace(char* str)
{
	char currChar;
	for(int i = 0; i < strlen(str); i++)
	{
		currChar = str[i];
		if(currChar != '@')
		{
			for(int j = i + 1; j < strlen(str); j++)
			{
				if(currChar == str[j])
				{
					str[j] = '@';
				}
			}
		}
	}
	moveZeros(str);
}

int main()
{
	char a[] = "ccbbbcdecdea";
	char b[] = "abcdabcdabcd";

	dupeWithNoExtraSpace(a);
	dupeWithExtraSpace(b);

	printf("%s \t %s \n",a , b);
}

The solution has two versions, one with extra space, and one without extra space. Without extra space, we need O(n!) time complex algo.
With extra space it is doen in O(n)

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

def dedupe(string):
    result = ""
    for i, letter in enumerate(string):
        if letter not in result:
            result += letter
    return result

- twigfiggly October 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Oops, correction:

def dedupe(input_str):
    result = ""
    for letter in input_str:
        if letter not in result:
            result += letter
    return result

- twigfiggly October 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your answer is O(n^2) but it can be done in O(n) using a hash. So, you wont check if the character is the string 'result', just validate if it is already a key in the hash

- merlinparrajimenez January 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java code

public static void main(String[] args){
	String s="abcdaabbcdefghk";
	String r=dedupe(s);
	System.out.println(r);
	}	
	
	static String dedupe(String s){
 	char[] c=s.toCharArray();
		for(int i=0;i<c.length;i++){
			char tmp=c[i];
			for(int j=0;j<c.length;j++){
				if(i==j)
					continue;
				else
					if(tmp==c[j])
						c[j]=' ';
			}
		}
		String result=String.valueOf(c);
		return result.replaceAll("\\s", "");
	}

- Youngsam September 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char* dedupe(char* str)
{
	char* strnew = new char;
	strnew[0] = '\0';

	int nLen = strlen(str);
	int nCount = 0;
	for(int i = 0 ; i <nLen; i++)
	{
		int nnLen = strlen(strnew);
		bool flag = 0;
		for(int j = 0 ; j < nnLen; j++)
		{
			if(strnew[j] == str[i])
			{
				flag = 1;	
			}

		}
		if(!flag)
		{
		
			strnew[nCount] = str[i];
			strnew[nCount+1] = '\0';
			nCount++;
		}
		

	}
	
	return strnew;
	
}

- hajamaideen January 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String dedupe(String s){
		if(s == null || s.length() < 2){
			return s;
		}
		
		boolean used[] = new boolean[256];
		Arrays.fill(used, false);
		
		StringBuffer sb = new StringBuffer();
		char[] sc = s.toCharArray();
		for(int i = 0; i < sc.length; i++){
			if(!used[(short) sc[i]]){
				sb.append(sc[i]);
				used[(short) sc[i]] = true;
			}
		}
		
		return sb.toString();
	}

- just_do_it March 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Java:

public static String cleanString(String arg) {
		
		String result = "";
		
		for (int i = 0; i < arg.length(); i++) {
			
			char c = arg.charAt(i);
			
			if (result.indexOf(c) == -1) {
				result += c;
			}
		}
		
		return result;
	}

- manuel April 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String cleanString(String arg) {
		
		String result = "";
		
		for (int i = 0; i < arg.length(); i++) {
			
			char c = arg.charAt(i);
			
			if (result.indexOf(c) == -1) {
				result += c;
			}
		}
		
		return result;
	}

- manuel April 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String cleanString(String arg) {
		
		String result = "";
	
		while (arg.length() > 0) {
			
			char c = arg.charAt(0);
			
			if (result.indexOf(c) == -1) {
				result += c;
				arg = arg.replace(String.valueOf(c), "");
			}
		}
		
		return result;

}

- Anonymous April 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String cleanString(String arg) {
		
		String result = "";
	
		while (arg.length() > 0) {
			
			System.out.println(" arg " + arg + " result " + result);
			
			char c = arg.charAt(0);
			
			if (result.indexOf(c) == -1) {
				result += c;
				arg = arg.replace(String.valueOf(c), "");
			}
		}
		
		return result;
	}

- ManuelMH50 April 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I have a phone interview with facebook.Any tips or suggestions?

- HelloWorld October 14, 2008 | 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