Citigroup Interview Question for Java Developers


Country: India




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

How about this one

public String moveChar(String string)
{
	if(string.length() ==0 || string.length() ==1)
	{
		return string;
	}
	else
	{
	       return (s.substring(s.length()-1).concat(s.substring(0,s.length()-1));
	}
}

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

Very nice solution, but probably not as space efficient as the solution above, that is you have to create three strings (one each for the two substrings and one for the concat). The solution above is in - place and requires no extra storage.

- Anonymous October 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

method sustring does not create new string insteade uses ogiginal string and the index limit to use hence no issue with space complexity.

Note: Check the internal working of substring

- PKT April 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class MoveChar{

	public String move(String input) {
		char[] arr = input.toCharArray();
		swap(arr, 0, arr.length-1);
		int i = arr.length - 1;
		while(i > 1) {
			swap(arr, i, i-1);
			i--;
		}
		return new String(arr);
	}
	private void swap(char[] arr, int x, int y) {
		char  c = arr[x];
		arr[x] = arr[y];
		arr[y] = c;
	}

}

swap the first and last char
then continue swapping neighbors till the second element in the char array

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

To use even less memory
- Instead of passing array and two indices pass two references.
- Replace your swap with an in place swap to avoid using temp.

- Phyxle October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class MoveChar{

	public String move(String input) {
		char[] arr = input.toCharArray();
		int i = arr.length - 1;
		while(i > 0) {
			swap(arr, i, i-1);
			i--;
		}
		return new String(arr);
	}
	private void swap(char[] arr, int x, int y) {
		char  c = arr[x];
		arr[x] = arr[y];
		arr[y] = c;
	}

}

- mengmeng October 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Hmm.. ok.

Best done if underlying data structure is a pointer linked data structure with a head and tail pointer available.
Then a rotate by 1 char is O(1).

If you want to go with an array,

temp=a[0]; 
for(int i=0; i<N-2; i++) a[i]=a[i+1]; 
a[N-1] = temp;

- S O U N D W A V E October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

or rotate other way :P

- S O U N D W A V E October 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Java,

public class JavaStringShift {

	public static void main(String[] args) {
		
		String str = "abcde";
		
		System.out.println("Input : " + str);
		str = strShift(str);
		System.out.println("Output : " + str);

	}
	
	public static String strShift(String str) {
		
		char[] chars = new char[str.length()];
		
		str.getChars(str.length()-1, str.length(), chars, 0);
		str.getChars(0, str.length()-1, chars, 1);
		
		return new String(chars);
		
	}

}

Input : abcde
Output : eabcd

- SJ Park October 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

easiest and most logical way.

- Sagar July 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MoveChar{

public String move(String input) {
char[] arr = input.toCharArray();
int i = arr.length - 1;
while(i > 0) {
swap(arr, i, i-1);
i--;
}
return new String(arr);
}
private void swap(char[] arr, int x, int y) {
char c = arr[x];
arr[x] = arr[y];
arr[y] = c;
}
}

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

def move(str1):
	return str1[-1]+str1[:len(str1)-1]

- rohan.rapidwolverine October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void moveString(String str){
char last = str.charAt(str.length()-1);
String temp = str.substring(0,str.length()-1);
String moved = last + temp;
System.out.println(moved);
}

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

static String shiftString(String str) {
        int length = str.length();
        char[] chars = new char[length];
        chars[0] = str.charAt(length-1);

        //a-0,b-1,c-2,d-3,e-4
        //e-0,a-1,b-2,c-3,d-4

        for (int i = 0, j = i + 1; i < length - 1; i++,j++) {
            chars[j] = str.charAt(i);
        }

        return String.valueOf(chars);
    }

Would this be efficient w.r.t space requirement?

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

static String shiftString(String str) {
        int length = str.length();
        char[] chars = new char[length];
        chars[0] = str.charAt(length-1);

        for (int i = 0, j = i + 1; i < length - 1; i++,j++) {
            chars[j] = str.charAt(i);
        }

        return String.valueOf(chars);
    }

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

{

public class Test{
String first="ABCDE";
int length=first.length();;
public void reverse(){
StringBuffer br=new StringBuffer();
br.append(first.charAt(length-1));
for(int i=0;i<length-1;i++){
br.append(first.charAt(i));

}
System.out.println(br.toString());
}
}
}

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

public class moveChar {
public static void main(String[] args){
String str = "abcde";
System.out.println("Input: " + str);
String out = move(str);
System.out.println("Output: " + out);
}
public static String move(String str){
char[] s = str.toCharArray();
StringBuilder strB = new StringBuilder();
strB.append(s[s.length-1]);
for (int i=0; i<s.length-1; i++)
{
strB.append(s[i]);
}
return (strB.toString());
}
}

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

public class Oness {

	public static void main(String[] args) {
		
		
		String s1="abcde";
		String s2="eabcd";
		System.out.println("1 st Approach ");
		
		for(int i=0;i<s1.length();i++)
		{
			String s=s1.substring(0,1);
			s1=s1.substring(1, s1.length());
			s1=s1+s;
			
			if(s1.equals(s2))
			{
				
				System.out.println("found at position  "+(i+1));
				break;
			}	
		}
		System.out.println("2nd Approach");
		
		String s5="abcde";
		
		String temp=s5.substring(s5.length()-1,s5.length());
		
		s5=s5.substring(0,s5.length()-1);
		s5=temp+s5;
		System.out.println(s5);
	}

}

OutPut :
1 st Approach 
found at position  4
2nd Approach
eabcd

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

public class Oness {

	public static void main(String[] args) {
		
		
		String s1="abcde";
		String s2="eabcd";
		System.out.println("1 st Approach ");
		
		for(int i=0;i<s1.length();i++)
		{
			String s=s1.substring(0,1);
			s1=s1.substring(1, s1.length());
			s1=s1+s;
			
			if(s1.equals(s2))
			{
				
				System.out.println("found at position  "+(i+1));
				break;
			}	
		}
		System.out.println("2nd Approach");
		
		String s5="abcde";
		
		String temp=s5.substring(s5.length()-1,s5.length());
		
		s5=s5.substring(0,s5.length()-1);
		s5=temp+s5;
		System.out.println(s5);
	}

}

OutPut :
1 st Approach 
found at position  4
2nd Approach
eabcd

- Mahesh March 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MoveStringChar {
	
	static String moveChars(String input){
		char[] output = input.toCharArray();
		for(int i=0; i<input.length();i++){
			if(i==0){
				output[i]= input.charAt(input.length()-1);
			}
			else{
				output[i] = input.charAt(i-1);
			}
		}
		return String.valueOf(output);
	}
	
	public static void main(String[] args){
		System.out.println(moveChars("abcde"));
	}

}

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

import java.util.*;
public class strin {

public static void main(String[] args) {

String st = "abcf";

ArrayList al=new ArrayList();

char d[]=st.toCharArray();

al.add(d[d.length-1]);

for (int i = 0; i < d.length-1; i++) {

al.add(d[i]);
}

System.out.println(al);

}
}

- P.vamsikrishna February 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

step 1: reverse the entire array
step2: reverse the first k
step3: reverse k+1 to end

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

String str = "abcde";
String out = str.charAt(str.length() - 1) + str.substring(0, str.length() - 1);
System.out.println("result: " + out);

- Anonymous September 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

public class CharMoveTest {
	public static void main(String[] args) {
		String str = "abcdef";
		char[] elements = str.toCharArray();
		char lastElement = elements[elements.length-1];
		for(int i=elements.length-1;i>0;i--){
			elements[i] = elements[i-1];
		}
		elements[0] = lastElement;
		System.out.println(elements);
	}
}

- Anonymous October 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

PROPER CODE AND OUTPUT:

import java.io.*;
public class Anonymous{

public static void main(String[] args){

char[] str = {'a','b','c','d','e'};
int i = str.length-2;
while(i >=0)
{
char tmp = str[i];
str[i] = str[i +1];
str[i+1] = tmp;
i--;
}
System.out.println(str);


}}

OUTPUT:

eabcd

- Anonymous October 16, 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