Goldman Sachs Sample Mock Test

I have attempted the questions , but didn't got them right , I am curious to know the solution.

Question 1:
An array is given with numbers, we have to find if can reach the value 0 or not.
Starting from the start index we can move left and right equal to the value at that index if only if there are possibility to move(the move should be in the array size) .

Complexity:
n=100

Input1:
array: 2 1 0 1 2 1 6
starting pos: 6

output : true

explanation:
index=6,arr[index]=1,move right by 1
index=7, arr[index]=6 , move left by 6
index=1, arr[index]=2 ,move right by 2
index=3, arr[index]=0.
reached 0 return true.

Input2: 4 2 0 2 3
starting pt : 4
**Output:**False

I have written the code but didnt passes all the test cases:

#include<bits/stdc++.h>
using namespace std;


 //cout<<dp[3];

bool ask(int arr[],int start,int n,int dp[]){
    
    if(arr[start-1]==0)
        return true;
  
 // cout<<dp[start]<<" ";
   
   if(dp[start]==0){

      dp[start]=1;
    if(start+arr[start-1]<=n)
     return ask(arr,start+arr[start-1],n,dp);
     
     if(start-arr[start-1]>0)
     return ask(arr,start-arr[start-1],n,dp);
    
    }

  return false;

}

int main(){
   
   int t;
   cin >>t;
   while(t--){
    int n;
    cin >> n;

    int arr[n];

 int dp[101]={0};
    for(int i=0;i<n;i++){
        cin >>arr[i];
    }

    int start;
    cin >>start;

    cout<<ask(arr,start,n,dp);
   } 
  
}

Queston2:

The test is the sample test of Goldman Sachs and the question mentioned is in the advanced section question set of the test.

Problem Statement:

A spy agency deciphers coded messages of the enemy by comparing the messages with known keywords. To check for a match they tweak both the message (a string) and the keyword (also a string) and then compare the two strings character by character. The only way to tweak a string is by introducing underscores in between the characters so as to move the characters. The aim is to perform the tweaking to yield the lowest mismatch score. The mismatch score is calculated as the sum of all character mismatches in the strings. The mismatch score x when an underscore is matched to a character is different from y, the mismatch score when two different characters are matched to each other.

Given the two strings, and the mismatch scores x and y, write a program to tweak the two strings to minimize the total mismatch score.

Constraints:

  1. 1 <= length of the strings <= 100

  2. 1 <= mismatch scores x, y < 10000

  3. x != y in them

  4. Input strings do not have underscores "_" in them.

Input Format:

The first line of input contains a string, the message.

The second line of input contains another string, the keyword.

The third and fourth lines of input contain the mismatch scores x and y respectively

Output Format: The output contains the minimum mismatch score.

Sample Input1:

draw

dew

2

3

Sample Output: 5

Explanation: To minimize the mismatch, underscore is introduced in the middle of dew' to make the first and last characters of both the strings match perfectly e.g. "d_ew" Now the second character ris matched with an underscore, so a mismatch score of 2 is added. Similarly, another mismatch score of 3 is added when the third characters are mismatched (a and e). So, the total mismatch score is 2+3= 5.

Sample Input2:

this is her text

the text

4

5

Sample Output: 32

Sample Input3:

sample

spm

2

200

Sample Output: 10

I got an approach of using the longest common subsequence and then arranging the element by backtrack but not sure and also don't how to know to implement the same.

And I want to also know if this question can be done in any other way that can handle short strings to run successfully for short test cases.

Comments (7)