Linkedin Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
7
of 9 vote

void recurreverse(char *t)
{
	if(*t == '\0') 
		return;

	recurreverse(t+1);
	cout << *t;
}

- chaitanya.jun12 March 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Java version

public class reverseStringRecursive {
	
	
	private static void reverse(char[] s,int start,int end){
		
		if(start > end){
			System.out.println("Reversed String " + String.valueOf(s));
			return;
		}
		if(start == end)
			return;
		
			char temp = s[start];
			s[start] = s[end];
			s[end] = temp;
			reverse(s,start+1,end-1);
			
		
	}
	
	public static void main(String args[]){
		
		String s = "This is a test";
		char []temp = s.toCharArray();
		reverse(temp,0,temp.length-1);
		System.out.println(s);
	}
	
}

- ps September 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

class StringReverse {
	public static void main(String []args) {
		System.out.println(revString("This is a test"));
	}
	
	static String revString(String str) {
		System.out.println(str);

		int length = str.length()-1; 

		if ( length ==0 )	
			return String.valueOf(str.charAt(0));

		if ( length == 1 )
			return String.valueOf(str.charAt(1)) + String.valueOf(str.charAt(0));

		return String.valueOf(str.charAt(length)) + revString(str.substring(1, length)) + String.valueOf(str.charAt(0));
		

	}
}

- PCB March 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public string RevertStr(string sIn)
{
if (string.isEmptyOrNull(sIn)) return "";
if(sIn.length == 1) return sIn;

char cbegin = sIn[0];
char cend = sIn[sIn.length-1];
return cend + RevertStr(sIn.substring(1, sIn.length-2)) + cbegin;
}

- seasons March 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class RevString
{
	

public String rev(String s)
{
if(s.length() == 0)
{
	return " ";
}
else
{
return s.charAt(s.length()-1)+ rev(s.substring(0, s.length()-1));
}
}


public static void main(String args[])
{
 RevString r = new RevString();
 String s = new String("This is a test");
 System.out.println(r.rev(s));
}
}

- Anonymous April 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class ReverseString
{

	public static void main(String[] args)
	{
		String test = "This is a test";
		System.out.println(test);
		System.out.println(reverseString(test));
	}

	public static String reverseString(String s)
	{
		if (s == null || "".equals(s))
		{
			return "";
		}

		int length = s.length();
		if (s.length() == 1)
		{
			return s;
		}

		return s.charAt(length - 1) + reverseString(s.substring(1, length - 1)) + s.charAt(0);
	}

}

- aviundefined August 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good & simple.

- purpleshoes November 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//// Its a Executed Code...
public class StringReverseRecursion
{
public static void main(String args[])
{
String s1 = "prashanth";
reverse(s1,0,s1.length()-1);
}
public static void reverse(String s,int i, int length)
{
if(i == length)
{// donothing
}
else
reverse(s.substring(1),i = i+1, length);
System.out.print(s.charAt(0));
}

}

- Prashanth March 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <string.h>

void f(char *s, char *rev, int i, int j, int size)
{
	if(*(s+i) == '\0') {
		*(rev+i+size) = '\0';
		return;
	}
	f(s, rev, i+1, j-1, size);	
	*(rev+j) = *(s+i);	
}

int main()
{
	char s[] = {"this is test"};
	char *rev = malloc(strlen(s));
	f(s, rev, 0, strlen(s), strlen(s));
	rev++;
	printf("revers is %s\n", rev);
	return 0;
}

- try March 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1 reverse the string O(n) time
2. then reverse each word,..
u can apply divide and conquore methde then apply same method to each word..

time complexity O(n).,

- pintuguptajuit(PINTU GUPTA) March 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String reverseString(String input)
	{
		if(input != null)
		{
			if(input.length() == 1)
			{
				return input;
			}
			String subStr = input.substring(0,input.length()-1);
			return input.charAt(input.length()-1) + reverseString(subStr);
		}
		return null;
	}

- Archana May 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function reverse(s){
    return s.split("").reverse().join("");
}
var test = "some string";
console.log(reverse(test));

- 8dgree June 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RevrString {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String s = "This is a string to revrese";
revstr(s,s.length()-1);
}

private static void revstr(String s, int last) {
// TODO Auto-generated method stub
if(last<0)return;
char c = s.charAt(last);
System.out.print(c);
revstr(s,last-1);


}

- Coder July 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are three types of problems possible with String reverse
1) Reverse the whole string:
Input: This is a test
Output: tset a si sihT
2) Reverse words in place
Input: This is a test
Output: sihT si a tset
3) Reverse only the words:
Input: This is a test
Output: test a is This

So for first problem just we need to divide this into smaller problem to reverse this string
Then second one can be done by just reversing the Sub strings in place
Then third one , just a combination of first and two. Reverse this string, then reverse the words in place.

package com.problems;

public class ReverseString
{

	public static void main(String[] args)
	{
		String test = " This  is a test ";

		System.out.println("Orignal String: " + test);

		char[] chars = reverseString(test.toCharArray(), 0, test.toCharArray().length - 1);
		System.out.println(String.valueOf("Reverse string: " + String.valueOf(chars)));
		
		
		chars = reverseStringWordsInPlace(test.toCharArray());
		System.out.println(String.valueOf("Reverse words in place: " + String.valueOf(chars)));
		
		chars = reverseStringWordsInPlace(chars);
		System.out.println(String.valueOf("Reverse only words: " + String.valueOf(chars)));

	}

	

	public static char[] reverseStringWordsInPlace(char[] chars)
	{
		int start = 0, end = 0;
		for (int i = 0; i < chars.length; i++)
		{
			if (" ".equals(String.valueOf(chars[i])))
			{
				reverseString(chars, start, i - 1);
				start = i + 1;
			}
		}

		reverseString(chars, start, chars.length - 1);

		return chars;
	}

	public static char[] reverseString(char[] chars, int start, int end)
	{
		if (end < start)
		{
			return chars;
		}

		if (chars.length == 0 || chars.length == 1)
		{
			return chars;
		}
		swap(chars, start, end);

		return reverseString(chars, ++start, --end);
	}

	public static char[] swap(char[] chars, int i, int j)
	{
		char temp = chars[i];
		chars[i] = chars[j];
		chars[j] = temp;
		return chars;
	}
	
	public static String reverseString(String s)
	{
		if (s == null || "".equals(s))
		{
			return "";
		}

		int length = s.length();
		if (s.length() == 1)
		{
			return s;
		}

		return s.charAt(length - 1) + reverseString(s.substring(1, length - 1)) + s.charAt(0);
	}
}

- aviundefined August 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

StringBuilder text = new StringBuilder("This is test");
        revString(text);
	
	private static void revString(StringBuilder text) {
		revRecu(text, 0, text.length() -1);
		System.out.println(text);
	}
	
	private static void revRecu(StringBuilder text, int i, int j) {
		if (i < j) {
			char temp = text.charAt(i);
			text.setCharAt(i, text.charAt(j));
			text.setCharAt(j, temp);
			revRecu(text, i+1, j-1);
		}
	}

- Big O September 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(string[] args)
        {
            string str = "1234567890";
            Console.WriteLine(reverst(str, 0));
        }

        static string reverst(string str, int pos)
        {
            if (pos == str.Length - 1)
            {
                return str[pos].ToString();
            }

            return reverst(str, pos + 1) + str[pos];
        }

- shrimpy September 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main (String[] args) throws java.lang.Exception
{
System.out.println(reverse("This is a test"));
}
public static String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
------------------------------
Output: tset a si sihT

- Wizi September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def foo(s):
     if len(s)==0:
             return ''
     else:
             return s[-1]+foo(s[:-1])

- Bling! September 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		System.out.println(reverseStringRecu("this is awesome!", new StringBuilder()));
    }
	
	private static String reverseStringRecu(String stringToReverse, StringBuilder sb) {
		
		if(stringToReverse == null || stringToReverse.trim().equals("")) {
			return stringToReverse;
		}
		int len = stringToReverse.length();
		if(len ==1 ){
			sb.append(stringToReverse);
			return sb.toString();
		}
		int last = len - 1;
		sb.append(stringToReverse.charAt(last));
		return reverseStringRecu(stringToReverse.substring(0, last), sb);
	}

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

def recursive_reverse(s):
	if len(s) <= 1: return s
	return recursive_reverse(s[1:]) + s[0]

- sola September 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void print(string& sentence, int i)
{
      if (i == sentence.size()) return;
      print(sentence, i+1);
      cout << sentence[i] ;
}

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

Solution in C:

#include <stdio.h>
#include <string.h>

#define SWAP(a,b) { char t = a; a = b; b = t; }

void rev_str(char* s, char *r)
{
    if(s < r - 1)
         rev_str(s+1, r-1);
    SWAP(*r,*s);
}
 
int main(void)
{
    char str[] = "This is a test";
    char *end = str + strlen(str) - 1;
    rev_str(str, end);
    printf("rev: %s", str);
    return 0;
}

- csol November 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void reverse(char a[], int low, int high) {
		if(low < high) {
			char ch = a[low];
			a[low] = a[high];
			a[high] = ch;
			reverse(a, low+1, high-1);
		}
	}
	public static void main(String[] args) {
		String str = "This is test";
		char a[] = str.toCharArray();
		reverse(a, 0, str.length()-1);
		System.out.println(new String(a));
	}

- Anand November 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{ /*
IIIT-HYD*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<vector>
using namespace std;
#define FOR(a,b,c) for(int a=b;a<c;a++)
#define sii(a,b) scanf("%d %d",&a,&b)
#define si(a) scanf("%d",&a)
#define pii(a,b) printf("%d %d\n",a,b)
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,1,sizeof(a))
string word="";
void rever(string a){
if(a.size()==0){
return;
}
rever(a.substr(1));
word=word+a[0];
}
int main(){
string a;
getline(cin,a);
rever(a);
cout << word << endl;
return 0;
}
}}

- THE BEST December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

void reverseString(char *str){

   if(*str == '\0')
    return ;

   reverseString(str+1);
   printf("%c", *str);


}

int main()
{
    printf("Reverse a string recursively\n");
    printf("\n Please enter a string to reverse \n");

    char *str= malloc(sizeof(100*sizeof(char)));

    scanf("%[^\n]s", str);

    reverseString(str);
    return 0;
}

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

public static String reverse(String test) {
		if (test == null || (test != null && test.length() <= 1)) {
			return test;
		}

		return test.substring(1) + (test.charAt(0));

	}

- Nilesh Salpe January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Typos Mistake

public static String reverseRecur(String str) {
	    if ((null == str) || (str.length()  <= 1)) {
	        return str;
	    }
	    return reverseRecur(str.substring(1)) + str.charAt(0);
	}

- Nilesh Salpe January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseStringRecursive {

static String reverseString(String input,int index,String returnString){
if(index<0){
return returnString;
}
else{
returnString =returnString+ input.charAt(index);
input = input.substring(0,index);
index--;
return reverseString(input,index,returnString);
}

}

public static void main(String[] args){
String input = "input string";
String retString ="";
int index = input.length()-1;
System.out.println(reverseString(input,index,retString));
}

}

- shobhittandon2003 August 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		StringBuilder sb = new StringBuilder("");
    	reverseString(sb, "this is a test", 14 );
    }


    public static void  reverseString(StringBuilder reversedString, String originalString, int length){
		if(originalString.length() == 0){
			System.out.println(reversedString);
			return;
		}
		reversedString.insert(0, originalString.charAt(0));
		String originalString2 = originalString.substring(1,length);
		reverseString(reversedString, originalString2, length-1);
	}

- Anonymous November 14, 2014 | 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