## Goldman Sachs Interview Question for Software Engineers

Country: United States

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

Can you clarify - couldn't understand the question.

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

c++, implementation, O(n)

``````int countOfMeltDown(vector<int>& arr) {
vector<int> mins;
int i, k, count;

mins.assign(arr.size(), 0);

count = 0;
for (i = 0, k = 1; i < arr.size(); i++, k++) {
mins[i] = min(arr[i], k);
if (arr[i] <= 0) k = 0;
}

for (i = arr.size() - 1, k = 1; i >= 0; i--, k++) {
count = max(count, min(mins[i], min(arr[i], k)));
if (arr[i] <= 0) k = 0;
}

return count;
}``````

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

``````public int meltHistogram1(int[] a){

int len = a.length;
int count = 0;
boolean flag = true;
while(flag){
flag = false;
int prev = a[0];
for(int j = 1; j < len-1; ++j){
int k = a[j];
if(k == 0)
k = 0;
else if(k == prev && k == a[j+1])
k = k - 1;
else if(k < prev && k < a[j+1])
k = k - 1;
else{
if(k > prev)
k = prev;
if(k > a[j + 1])
k = a[j + 1];
}
prev = a[j];
a[j] = k;
}
a[0] = 0;
a[len-1] = 0;
count++;
for(int i = 0; i < len; ++i){
flag = flag || (a[i] != 0);
}
}

return count;``````

}

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

``````public int meltHistogram1(int[] a){
int len = a.length;
int count = 0;
boolean flag = true;
while(flag){
flag = false;
int prev = a[0];
for(int j = 1; j < len-1; ++j){
int k = a[j];
if(k == 0)
k = 0;
else if(k == prev && k == a[j+1])
k = k - 1;
else if(k < prev && k < a[j+1])
k = k - 1;
else{
if(k > prev)
k = prev;
if(k > a[j + 1])
k = a[j + 1];
}
prev = a[j];
a[j] = k;
}
a[0] = 0;
a[len-1] = 0;
count++;
for(int i = 0; i < len; ++i){
flag = flag || (a[i] != 0);
}
}

return count;
}``````

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

``````public int meltHistogram(int[] a){
int len = a.length;
int count = 0;
boolean flag = true;
while(flag){
flag = false;
int prev = a[0];
for(int j = 1; j < len-1; ++j){
int k = a[j];
if(k == 0)
k = 0;
else if(k == prev && k == a[j+1])
k = k - 1;
else if(k < prev && k < a[j+1])
k = k - 1;
else{
if(k > prev)
k = prev;
if(k > a[j + 1])
k = a[j + 1];
}
prev = a[j];
a[j] = k;
}
a[0] = 0;
a[len-1] = 0;
count++;
for(int i = 0; i < len; ++i){
flag = flag || (a[i] != 0);
}
}

return count;``````

}

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

``````int countHist(int arr[],int n)
{
for(int i=0;i<n;i++)
if(arr[i]>=5)
arr[i]=0;
int maxval=0;
for(int i=0;i<n;i++)
maxval=max(max,val);
return maxval;
}``````

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

What does melt mean in this context?

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

``````package goldmansachs;
import java.util.*;
import java.math.*;
public class histogramMelt {

public static void main(String args[]){
//int arr[] = {5,4,3,6,0,1};
int arr[] = {0,1,1,1,1,0};
int count =histogramMelt(arr);
System.out.println("result =" +count);
}

public static boolean checkZero(int arr[]){
int flag =0;
for(int i=0;i<arr.length;i++){
if(arr[i]==0){
flag=1;
}
else{
flag=0;
break;
}
System.out.print(arr[i] +" ");
}
if(flag==0) return false;
else return true;
}

public static int  histogramMelt(int arr[]){
boolean b ;
int count=0;
int l = arr.length;
int val[] = new int[l];
do{
for(int i =0 ; i<l;i++){
System.out.println(i+" "+arr[i]);
if(i==0 || i== l-1){ //corner case first and last elements of the array
val[i]=0;
System.out.println(i+" "+val[i]);
}
else{
if((arr[i]>arr[i+1])||(arr[i]>arr[i-1])){ // other values are replaced by min(left,right) neighbours
val[i] = Math.min(arr[i+1],arr[i-1]);
System.out.println(i+" "+val[i]);
}
else{

val[i] = arr[i]-1; // every column has the top block exposed, so every column gets deducted by 1
if(val[i]<0) val[i]=0; // avoid negatives
System.out.println(i+" "+val[i]);
}
}

}

b =checkZero(val);
arr =val;
count ++; // number of times the histogram was melted
} while(b==false);
return count;
}
}``````

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

Ruby

``````def melt_histogram(array)
count = 0
while !array.all?(&:zero?)
prev = 0
array.each_with_index do |x, i|
reduce_to = [prev, array[i+1].to_i].min
array[i] = [0, [reduce_to, x-1].min].max
prev = x
end
count +=1
end
count
end``````

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

can someone please explain the question and the logic behind the solution?

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

can you explain the logic behind the solution?

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.

### 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.