Samsung Interview Question for Senior Software Development Engineers


Country: United States
Interview Type: In-Person




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

A> Reverse the whole string first.
B> Reverse individual words of the output string from step A.

char* ReverseAllWords(char* text) {
    int lindex = 0;
    int len = strlen(text) - 1;
    if (len > 1) {
	
        //reverse complete string
        text = ReverseString( text, 0, len );

        //reverse each word
        for (int rindex = 0; rindex <= len; rindex++) {
            if (rindex == len || text[rindex] == ' ') {
                text = ReverseString(text, lindex, rindex - 1);
                lindex = rindex + 1;
            }
        }
    }
    return text;
}

char* ReverseString(char* str, int begin, int end) {
    while (begin < end) {
        char tempc = str[begin];
        str[begin++] = str[end];
        str[end--] = tempc;
    }
    return str;
}

- R@M3$H.N September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Steps to solve this problem:
1) Start from the end of the string.
2) Iterate each character until an empty char.
3) When an empty char is found, keep that index and from that index to the end copy the string into temp.
4) do step 3 untill you reach start of the original string.

C# code for same;

private static void ReverseAStringByWords()
{
string s = "Reverse this string by words";
string temp = string.Empty;
int k = s.Length;
int n = 0;

for (int i = k; i >= 0; i--)
{
if (i == 0 || s[i - 1] == ' ')
{
for (int j = i; j < k; j++)
{
temp += s[j];
}
temp += ' ';
k = i;
}
}
Console.WriteLine(temp);
}

- Sayantan Samanta September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution works. However, it violates the instructions laid out by the interviewer.

"Can’t use any other data structures or buffer. Can only use a char temp."

You

string temp

is a buffer. You can only use a

char temp

.

- Ronnie September 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this, it is java.

public static void main(String[] args){
		
		char[] cc={'N','o','r','t','h','e','r','n',' ','C', 'a', 'l', 'i','f', 'o','r','n','i','a',' ','U','S','A'};
		
		int start=cc.length-1;
		for(int i=cc.length-1; i>=0; i--){
			
			if(cc[i]==' ' || i==0)
			{
				for(int j=i; j<=start; j++){
					System.out.print(cc[j]);
				}
				start=i;
			}
		}
		
	}

- ATuring September 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Won't that make the char[] "nrehtroN ainrofilaC ASU" ?

- zortlord October 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SentenceReverse {
    private static void printReverse(char[] chars){
        int end  = chars.length;
        
        for(int i = chars.length - 1; i >= 0; i--){
            if(i == 0 || chars[i-1] == ' '){
                int k = i;
                while(k < end){
                    System.out.print(chars[k]);
                    k++;
                }
                end = i;
                System.out.print(" ");
            }
        }
    }
    public static void main(String[] args) {
        printReverse("Northern California USA".toCharArray());
    }
}

- TechnoKing October 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *rev(char* s){
	char *temp = malloc(strlen(s)*sizeof(char));
	int cnt=0,j=0,i;
	for(i=strlen(s);i>0;i--){
		if(s[i]=='\0'){
			//copy from cnt to i to cnt
			for(k=cnt;k<=i;k++){
				temp[j]=s[k];
				j++;
			}
			cnt =i;
		}
	}	
return temp;
}

- Anonymous November 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(i = S.length -1 ;i>=-1;i--){
if(S[i] == ' ' || i == -1){
j = i + 1 ;
while(j!= S.length && S[j]!=' '){
console.log(S[j]) ;
j++ ;
}
console.log(' ');
}
}

Solution without using any char temp.

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

for(i = S.length -1 ;i>=-1;i--){
    if(S[i] == ' ' || i == -1){
      j = i + 1 ;
      while(j!= S.length && S[j]!=' '){
	console.log(S[j]) ;
       j++ ;
       }
console.log(' ');
     }
}

Solution without any char temp.

- Anonymous December 15, 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