Goldman Sachs Interview Question for Software Engineers


Country: India
Interview Type: In-Person




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

Dutch national flag problem

- Sithis April 08, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use counting sort.

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

package com.program;

public class Sort0s1s2s {

public static void main(String[] args) {

int arr[] = { 0, 1, 1, 0, 1, 2, 1,
2, 0, 0, 0, 1 };
int n = 12;
sort012(arr, n);
for(int i=0;i<n;i++) {
System.out.print(arr[i]+" -");
}
}

private static void sort012(int[] arr, int n) {

int count0s=0;
int count1s=0;
int count2s=0;

for(int i=0;i<n;i++) {
if(arr[i]==0) {
count0s++;
}
if(arr[i]==1) {
count1s++;
}
if(arr[i]==2) {
count2s++;
}
}
for(int i=0;i<count0s;i++) {
arr[i]=0;
}
for(int i=count0s;i<count0s+count1s;i++) {
arr[i]=1;
}
for(int i=count0s+count1s;i<count0s+count1s+count2s;i++) {
arr[i]=2;
}

}

}

- sanjay April 25, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think it is a bit simpler than the "Dutch national flag problem".
It resolves to range count instead of high/mid/low sort.

void CountSort(int beg, int end, std::vector<int> v)
{
	std::vector<int> counts(end - beg + 1, 0);
	for (int i : v)
		counts[i - beg]++;
	auto start = v.begin();
	for (int i = beg; i <= end; i++)
              start = std::fill_n(start, counts[i - beg], i);
}

- hb May 04, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ArrangeZeroOneTwo {
public static void main(String arv[]) {
int arr[] = {1,0,1,2,0,2,0,2,1,0,2,1,0,1,1,1,1,1,2,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,0,0,0,0,0,0};
int pointerForZero=0;
int pointerForTwo=arr.length-1;
for(int i=0;i<arr.length && i<pointerForTwo ;i++) {
if(arr[i] == 0) {
arr[i] = arr[pointerForZero];
arr[pointerForZero] = 0;
++pointerForZero;
}
if(arr[i] == 2) {
arr[i] = arr[pointerForTwo];
arr[pointerForTwo] = 2;
--pointerForTwo;
--i;
}
}
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i] +" ");
}
}
}

- KISHOR July 04, 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