Goldman Sachs Interview Question
InternsCountry: India
Interview Type: In-Person
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...
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).
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;
}
}
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++;
}
}
}
}
#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;
}
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;
}
}
}
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;
}
}
}
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");
}
}
}
#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;
}
This looks a little bit like a zigzag problem:
The length of the repeated pattern is 8, so
or, we can count from 0:
and
- Will August 18, 2013