Goldman Sachs Interview Question for Interns


Country: India
Interview Type: In-Person




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

This looks a little bit like a zigzag problem:

1    9
2   810
3  7 11
4 6  12
5    13...

The length of the repeated pattern is 8, so

x = n%8
if x == 0 then
  return 2
else if x < 5 then
  return x
else
  return 10-x
end

or, we can count from 0:

0    8    16
1   79   15
2  6 10  14
3 5  11 13
4    12

and

x = (n-1)%8
if x < 5 then
  return x+1
else
  return 9-x
end

- Will August 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Excellent! +1

- Murali Mohan August 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great solution! Elegant!

- mahesh_ostwal@yahoo.in August 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome. Works perfectly

- Michelle August 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 votes

doesnot work if n =5 , it gives x =5 , but actually it is 4

- Shubhendu July 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

["thumb", "index", "middle", "ring", "little", "ring", "middle", "index"][n % 8 - 1]

- Anonymous August 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

K=((n-5)/4)+1+1;

Switch(n)
{
case 1: print "thumb 1"; break;
case 2: print "fingure 2"; break;
case 3: print "fingure 3"; break;
case 4: print "fingure 4"; break;
case 5: print "fingure 5"; break;
Default: If ((n-1)% 8==0) print "thumb 1";
ElseIf ((n-5)%8==0) print "fingure 5";
ElseIf (n%2==1 && n%4==0) print "middle fingure 3";
ElseIf (n%4==0 && k%2==1) print "fingure 4";
ElseIf (n%4==0 && k%2==0) print "fingure 2";
ElseIf (n%2==0 && k%2==0) print "fingure 4";
ElseIf (n%2==0 && k%2==1) print "fingure 2";
}

Hope it works...

- Ankit Gadia August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

take a map to store index values from 1 to 5.

int returnIndex(int n){
	int x=0,countLeft=0;
	for(int i=1;i<=n;i++){
		if(countLeft==0){
			x++;
			if(x>5) {x-=2;countLeft=1;}
		}else{
			x--;
			if(x<1) {x+=2;countLeft=0; }
		}
	}
	return x;	
}
print value maped to xth index in Map. time complexity-o(n).

- ajit August 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

my mistake in above code. condition is if(x>=5) and (x<=1)

- ajit August 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

["thumb", "index", "middle", "ring", "little", "ring", "middle", "index"][(n-1) % 8]

where n>=1

- edlai August 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Easiest approach is you create a doubly linked list with 5 nodes. Traverse it from First to Last then last to first direction and when you reach your N, return that node.

- billu August 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void identifyFinger(int n)
{
    if(n%2 != 0)
    {
        if( (n-1)%8 == 0)
            std::cout<<"Thumb Finger" <<std::endl;
        else if((n-5)%8 == 0)
            std::cout<<"Little Finger" <<std::endl;
        else
            std::cout<<"Middle Finger" <<std::endl;
    }
    else
    {
        int val = (n-2)%8;
        if(val == 0 || val == 6)
            std::cout<<"Index Finger"<<std::endl;
        else
            std::cout<<"Ring Finger" <<std::endl;
    }
}

- ganapathiraju.subbu August 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void find_finger(int n)
{
        bool dir;
        int t;
        string val[]={"Thumb", "Index", "Middle", "Ring", "Little"};
        if(n<6)
        {
                cout<<val[n-1];
                cout<<endl;
                return;
        }
        n=n-6;
        t=n/4;
        if(t%2==0)
        {
                dir=true;
        }
        t=n%4;
        if(dir==true)
        {
                cout<<val[4-t-1];
        }
        else
        {
                cout<<val[t+1];
        }
        cout<<endl;
}

- Akhilesh Pandey August 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just substract 1 fron number and check modulo with 5

n=n-1
k=n/4
m=n%4
if m is odd then 5-m
else 1+m

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

sorry if k is odd not m

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

Following code in Java works:

import java.util.*;

public class FingerCounting {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		LinkedList<String> ll = new LinkedList<String>();
		ll.add("Thumb");
		ll.add("Index");
		ll.add("Middle");
		ll.add("Ring");
		ll.add("Little");
		ListIterator<String> it = ll.listIterator();
		
		
		
		
		
		
		int n=6;int i=0;
		while(i<=n){
			while(it.hasNext()){
				if(i==n){
					System.out.println(it.next());
					return;
				}
				it.next();
				i++;
			}
			while(it.hasPrevious()){
				if(i==n){
					System.out.println(it.previous());
					return;
				}
					
				it.previous();
				i++;
				
			}
				
		}

	}

}

- devenster August 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

x=n%8
if(x==5)
then it is little finger;
if(x==1)
then it is thumb;
if(x==2||x==0)
then it is index;
if(x==3||x==7)
then it is middle;
if(x==4||x==6)
then it is ring;
if(n%8)

- purusharth August 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
int main()
{	
	int n;
	printf("Enter the index number");
	scanf("%d",&n);
	if(n==0)
	{
		printf("Please enter the number between 1...n \n");
		exit(0);
	}
	switch(n%8)
	{
		case 0:
		case 2:				
			printf("Index");break;
		case 1:
			printf("Thumb");break;
		case 3:
		case 7:
			printf("Middle");break;
		case 4:
		case 6:
			printf("Ring");break;
		case 5:
			printf("Little");break;
			
	}
	return 0;	
}

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

import java.util.*;

public class FingerCounting {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter the number:");
n=sc.nextInt();

switch(n%8){
case 0:
case 2:
System.out.println("Index");break;
case 1:
System.out.println("Thumb");break;
case 3:
case 7:
System.out.println("Middle");break;
case 4:
case 6:
System.out.println("Ring");break;
case 5:
System.out.println("Little");break;
}
}
}

- Kiran Reddy October 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;

public class FingerCounting {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter the number:");
n=sc.nextInt();

switch(n%8){
case 0:
case 2:
System.out.println("Index");break;
case 1:
System.out.println("Thumb");break;
case 3:
case 7:
System.out.println("Middle");break;
case 4:
case 6:
System.out.println("Ring");break;
case 5:
System.out.println("Little");break;
}
}
}

- Kiran Reddy October 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package finger; // another method

import java.io.*;

class testq1
{
public static void main(String args[]) // 1 2 3 4 5
// 9 8 7 6
{ // 10 11 12 13
int n=29; // input
get_the_finger(n);

}
public static void get_the_finger(int n)
{
if(n>5)
{
n=n-5;
int row_dir =(n-1)/4;
if((row_dir)%2==0)
{
if(n%4==0)
System.out.println("A");

if(n%4==3)
System.out.println("B");
if(n%4==2)
System.out.println("C");
if(n%4==1)
System.out.println("D");
}
else
{
if(n%4==0)
System.out.println("E");

if(n%4==3)
System.out.println("D");
if(n%4==2)
System.out.println("C");
if(n%4==1)
System.out.println("B");
}

}
else
{
if(n%5==0)
System.out.println("E");
if(n%5==4)
System.out.println("D");
if(n%5==3)
System.out.println("C");
if(n%5==2)
System.out.println("B");
if(n%5==1)
System.out.println("A");

}

}
}

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

#include<stdio.h>
int count(int n);
void main(){
int n;
printf("Enter the value");
scanf("%d",&n);
int res=count(n);
printf("The finger will be %d\n",res);
}
int count(int n){
int c=0,head=0;
while(n>0){
if(head==0){
c++;
if(c>=5)
head=1;
}
else{
c--;
if(c==1)
head=0;
}
printf("%d\n",c);
n--;
}
return c;
}

- Jatin Aggarwal September 11, 2017 | 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