Microsoft Interview Question






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

Rotatstring(char *str, int k)
{
Reverse(str[0], str[k-1]);
Reverse(str[k], str[n]);
Reverse(str[0], str[n]);



}

where n is lenth of string.

- vijay September 12, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Excellent solution!

- russoue February 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *rotatestring2(char * input, int diff)
{
char temp;
char oldvalue;
int index;
int len;
int newpos=0;
int oldpos=0;

len=strlen(input);

oldvalue=input[0];
for(index=0;index<len;index++)
{
newpos=oldpos+diff;
temp=input[newpos%len];
input[newpos%len]=oldvalue;

oldpos=newpos;
oldvalue=temp;
}
return input;
}

- bug November 13, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code looks cool but doesn't work for all cases.
Its behavior heavily depends on len%diff.

- Carbon April 02, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do an inplace reverse till the middle char.

- Vamsi December 06, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Stringrotation
{
class Stringrotation
{
static void Main()
{
string str;
Console.WriteLine("Enter the string to rotate ");
str = Console.ReadLine();
Console.WriteLine("Enter the number of rotations ");
int rot = Convert.ToInt32(Console.ReadLine());
int strlen = str.Length;
string outputstr = string.Empty;
outputstr += str.Substring(strlen - rot , rot);
outputstr += str.Substring(0, strlen - rot);

Console.WriteLine("The rotated string is {0} ", outputstr);
Console.Read();
}
}
}

- MSGeek March 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if we can use extra memory, then we can do like this
given string str: abcde
make temp : abcdeabcde \\str+str
rotation =3
return temp[3]temp[4]...temp[2*strlen-3] \\cdeab

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

k = k mod len
newstr = concat(str[len - k ... len - 1], str[0, len - k - 1])

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

public static void rotationOfString(String s, int n) {

if(s.length()<1)
{
return;
}
char[] characters = s.toCharArray();
char characterToPlace = characters[0];
for (int i = 1; i < characters.length; i++) {
char tempCurr = characters[i];
characters[i] = characterToPlace;
characterToPlace = tempCurr;
}
characters[0] = characterToPlace;


System.out.println("str :"+s+" rot :"+new String(characters));

}

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

public class HelperFunctions {

public static String rotateString(String s, int n) {

StringBuffer result = new StringBuffer();

//Rotate right
if(n>0) {

int rotatedChars = s.length()-n;
result.append(s.substring(rotatedChars, s.length())
+ s.substring(0, rotatedChars));

} else if(n<0) { //Rotate left

//Get positive value for shifting bits
int m = n*-1;
result.append(s.substring(m, s.length())
+ s.substring(0, m));

} else { //For n = 0 case, original string is ouput
result.append(s);
}


return result.toString();
}

public static void main(String[] args) {

System.out.println("\nrotateString(\"Pineapple\" , 1) : "+rotateString("Pineapple" , 1) );
System.out.println("rotateString(\"Pineapple\" , 2) : "+rotateString("Pineapple" , 2) );
System.out.println("rotateString(\"Pineapple\" , -1) : "+rotateString("Pineapple" , -1) );


}
}

- UdayKumar R September 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String rotateString(String s, int n) {

StringBuffer result = new StringBuffer();

//Rotate right
if(n>0) {

int rotatedChars = s.length()-n;
result.append(s.substring(rotatedChars, s.length())
+ s.substring(0, rotatedChars));

} else if(n<0) { //Rotate left

//Get positive value for shifting bits
int m = n*-1;
result.append(s.substring(m, s.length())
+ s.substring(0, m));

} else { //For n = 0 case, original string is ouput
result.append(s);
}


return result.toString();
}

- UdayKumar R September 10, 2016 | 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