Citigroup Interview Question for Java Developers


Team: Risk Management/CMOT
Country: India
Interview Type: In-Person




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

Approach:

1. Numbers will be between 1 & 100. So, create an array of integers - tmp[] - of size 100
2. Array tmp[] keeps track of repetitions in the input array - input[].
3. Initialize each element of tmp[] to 0
3. Traverse input[], increment the corresponding index in tmp[]
4. Create an array of the same size as input array - input[]. Call it result[]
5. Traverse tmp[] and get 'count' for each.
6. If count == 0, continue
7. Else, add the 'index' those many times into result[]
8. Finally, return result[]

- Ajith January 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Tried following with above algo. working fine for me.. :)

public class ArraySortPerformance {
//Array Length not specified in problem. Lets say n=50
private final static int ARRAY_LENGTH = 50;
public static void main(String[] args) {

//Prepare Input Data
int[] inputArray = new int[ARRAY_LENGTH];
Random random = new Random();
for(int i=0;i<50;i++){
inputArray[i] = random.nextInt(100);
}

//Create Temporary array
int[] tempArray = new int[101];
for(int i: inputArray){
tempArray[i]++;
}

//Sort Result based on temp
int[] resultArray = new int[ARRAY_LENGTH];
for(int i=1, j=0; i<=100;i++){
if(tempArray[i] == 0)
continue;
else{
for(int k=0; k<tempArray[i];k++){
resultArray[j] = i;
j++;
}
}
}

//Print input and output for verification
System.out.println("Input::");
for(int j: inputArray){
System.out.print(j+" ");
}
System.out.println("");
System.out.println("Result::");
for(int j: resultArray){
System.out.print(j+" ");
}
}
}

- LateRunner February 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

by using insertion sort.If the number we are inserting are not in reverse order ,then the time complexity is O(n).

- Sneha January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But that will be the best case of insertion sort. It will take O(n^2) in average and worst case.

- Kunal Saxena December 03, 2017 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Time Complexity : O(n)
Space Complexity : Input Range (works for only +ve numbers)

Data Structure Used : Hash table[1..100]

Algorithm :

1. Initialize Hash table[1...100] with 0.
2. Now Read values one by one in a variable index
// read----> index
3. Now increment the value in hash table at location of index
// hash_table[index]++;
4. If input is NOT over then go to step 2
else go to next step.
5. Now traverse the whole hash_table and print 'index' simultaneously wherever we get the value greater than 0.
6. STOP
//it will also keep the duplicates

- v.krishna1708 January 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If n >= 100, the problem can be solved in O(n) time and O(1) space by in-place counting. Java ode shown as below:

public void sort(int[] arr) {

	int mod = 128;
	int mask = mod - 1;

	for (int i : arr) {
		arr[(i & mask) - 1] += mod;
	}

	for (int i = 99, last = arr.length - 1; i >= 0; --i) {
		int count = arr[i] / mod;
		while (count-- > 0) {
			arr[last--] = i + 1;
		}
	}
}

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

Number range is fixed..100..
create a array result[100].
scan input array.
add +1 in result // result[ inputarray[i] - 1] += 1;
return result[].

- sudBisht January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.


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