Facebook Interview Question for Software Engineer / Developers






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

char* remove(char* str)
{
   char* writer=str,*reader=str;
   char lastchar='.';
   while(*reader)
   {
     if(*reader=='/'){
        if(lastseen!='/'){
          *writer++=*reader;
          lastseen=*reader;
        }
        *reader++;
      }
     else{
        *writer++=*reader;
        lastseen=*reader++;
     }	   
   }
   *writer='\0';
   return str;
}

- XYZ March 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

dude, char *writer,reader? what about the question mentioning about *no extra memory*?

- Anonymous March 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude...Do u understand the meaning of extra memory?? writer and reader are just pointers pointing to memory location of "str". Even if you don't understand just used integers for indexing rather than using pointers

- XYZ March 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

XYZ is correct...Anonymous, how else are you supposed to work with the data if not with pointers or integers identifying indices??

No extra memory simply means don't try to copy the string into a new string..do everything in-place is all.

- woohoo March 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include <stdio.h>

char* removeSlash (char *str) {
  int rindex = 0, windex = 0 ;
  char lchar = ' ' ;
  while ( str[rindex] != '\0' ) {
    if ( str[rindex] == '/' ) {
      if ( lchar != '/' )
        str[windex++] = str[rindex] ;
    } else { str[windex++] = str[rindex] ; }
    lchar = str[rindex++] ;
  }
  str[windex] = '\0' ;
  return str ;
}

int main () {
  char str[] = "/root//foo/bar" ;
  printf ( "\n%s\n", removeSlash (str) ) ;
  return 0 ;
}

- Psycho October 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this has to be solved by scripting instead of writing a program. just use a grep and pipeline it to convert it to the desired string.

- swinginigiant March 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what does that have to do with the question?

- woohoo March 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you rewrite below code using c-strings you could make space complexity O(1).


/**
	 * Time: O(N)
	 * Space: O(1)
	 */
	public String removeDuplicateSlashes(String str){
		
		if( str == null ){
			throw new IllegalArgumentException("NULL str passed");
		}		
		
		int copyIndexOffset = 0;
		
		// Space: O(N) make array copy
		final char[] arr = str.toCharArray();
		
		boolean previouslySlashFound = false;
		
		for( int i = 0; i < arr.length; i++ ){
			
			if( arr[i] ==  '/' ){
				if( previouslySlashFound ){
					continue;
				}				
				previouslySlashFound = true;
			}
			else {
				previouslySlashFound = false;
			}			
			
			arr[copyIndexOffset++] = arr[i];
		}
		
		// Space: O(N) allocate new array
		return new String(arr, 0, copyIndexOffset);		
	}

- m@}{ March 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree with XYZ algorithm...

- m@}{ March 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Would a regex solution consume significant amounts of extra memory?

- Spoom March 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can use sed command instead of the program or we can use a shell script which is just of 2 or 3 lines

- parmeshwar March 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about this

public static String shift(String s, int start, int count) {
		StringBuffer sb = new StringBuffer(s);
		int shiftStartIndex = start + count;
		if (shiftStartIndex >= sb.length()) {
			return s;
		}
		while (shiftStartIndex < sb.length()) {
			sb.setCharAt(start, sb.charAt(shiftStartIndex));
			++start;
			++shiftStartIndex;
		}
		sb.setLength(start);
		return sb.toString();
		
	}
	
	public static String removeSlash(String s) {
		if (s == null || s.length() == 0) {
			return s;
		}
		int i = 0;
		while ( i < s.length()) {
			char c = s.charAt(i);
			if (c == '/') {
				int slashIndex = i;
				++i;
				if (i == s.length()) {
					return s;
				}
				while ((c = s.charAt(i)) == '/') {
					++i;
				}
				int numShift = i - slashIndex - 1;
				if (numShift != 0) {
					s = shift(s, slashIndex+1, numShift);
					i = slashIndex + 1;
				}
			} else {
				++i;
			}
		}
		return s;
		
	}

- anon January 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Keep two pointers
One to move forward in Input-String characters and another to rewrite the Input-String. we rewrite only if current read char is not slash, if it's slash then check for previous char's being slash.
Keep a variable to maintain state, possible states - last-char-slash, last-char-not-slash.

- Sem February 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actually according to the question, only minimum no. of elements need to be moved. But the algorithms mentioned above always move the elements to the right of the current duplicate slash, that is wrong.

For ex: /foo//baaaaaaaaaaaar. Here when we encounter the duplicate slash we are moving the string "baaaaaaaaaaaar" to its left, and that is not minimum. Ideally, the string "/foo/" needs to be moved to its right.

- turbokarthik March 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is an algorithm.

1. First count the total number of non-duplicate elements (non-de-count)
2. Now iterate through the array, whenever we encounter a sequence of duplicate slashes i.e. one or more slashes, calculate the non-duplicate elements to its left and non-duplicate elements to its right. How?
the current index will give the no. of non-duplicate elements to its left and non-de-count-current will give the non-duplicate elements to its right.
3. if(non-de-left <= non-de-right){
shift left characters to the right;
}else{
call removeDuplicates(str, currentStart,end);
shift right characters to the left;
}

- turbokarthik March 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here is a very simple O(n) algorithm using O(1) space


void rem_dup(char *a)
{
int n=strlen(a);
int s=0,e=0;

for(int i=0;i<n;++i)
{
if(a[i]=='/')
{
s=i;
while(a[i]=='/' && i<n) ++i;
e=i;
for(int j=s+1;j<e;++j) a[j]='\0';
}
}

int pos=0;
for(int i=0;i<n;++i)
{
if(a[i]!='\0')
{
a[pos]=a[i];
pos+=1;
}
}
a[pos]='\0';
}

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

here is a very simple O(n) algorithm using O(1) space


void rem_dup(char *a)
{
int n=strlen(a);
int s=0,e=0;

for(int i=0;i<n;++i)
{
if(a[i]=='/')
{
s=i;
while(a[i]=='/' && i<n) ++i;
e=i;
for(int j=s+1;j<e;++j) a[j]='\0';
}
}

int pos=0;
for(int i=0;i<n;++i)
{
if(a[i]!='\0')
{
a[pos]=a[i];
pos+=1;
}
}
a[pos]='\0';
}

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

void delSlash(char *s)
{
	if (s == NULL)
		return;

	int i = 1;
	int j = 1;
	int len = strlen(s);

	if (len == 0)
		return;

	while(j < len)
	{
		if (s[j] == '/' && s[i-1] == '/')
			j++;
		else
		{
			s[i] = s[j];
			i++;
			j++;
		}
	}
	s[i] = '\0';
}

- remlostime October 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void RemoveDuplicateSlashes(char * str)
{
    int l = strlen(str);
    int p = 0;
    for(int i=0;i<l;i++)
    {
        if(i+1 < l && str[i] == '/' && str[i+1] == '/')
        {
            p++;
        }
        str[i] = str[p];
        p++;
    }
}

- Isaac Levy January 30, 2013 | 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