Goldman Sachs Advance Coding Questions

Goldman Sachs Advanced Coding Questions And Answers

Advance Coding Question section is an additional section in the Round 2 of Goldman Sachs hiring process. This section is an additional section after the basic coding round. This round is fairly difficult that the first round, and the questions asked in this round are from dynamic programming or greedy approach, unlike data structures which are asked in the first round.

Goldman Sachs Advance Coding Question and Answers
Goldman Sachs Advance Coding Number of questions

No. of Question
1 Question

Goldman Sachs Advance Coding difficulty level

Difficulty
High

Time Limit

Time Limit
30 mins

importance

Importance
High

Goldman Sachs Practice Coding Questions

Question 1

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes.

Input

  • The single line of the input contains a pair of integers m, s (1 ≤ m ≤ 100, 0 ≤ s ≤ 900) — the length and the sum of the digits of the required numbers.

Output

  • In the output print the pair of the required non-negative integer numbers — first the minimum possible number, then — the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers “-1 -1” (without the quotes).

 

Examples

  • Input 1
    2 15
  • Output 1
    69 96

 

  • Input 2
    3 0
  • Output 2
    -1 -1

 

Question 2

Karan got bored, so he invented a game to be played on paper. He writes n integers a1, a2, …, an. Each of those integers can be either 0 or 1. He’s allowed to do exactly one move: he chooses two indices i and j (1 ≤ i ≤ j ≤ n) and flips all values ak for which their positions are in range [i, j] (that is i ≤ k ≤ j). Flip the value of x means to apply operation x = 1 – x.
The goal of the game is that after exactly one move to obtain the maximum number of ones. Write a program to solve the little game of Iahub.

Input

  • The first line of the input contains an integer n (1 ≤ n ≤ 100). In the second line of the input there are n integers: a1, a2, …, an. It is guaranteed that each of those n values is either 0 or 1.

Output

  • Print an integer — the maximal number of 1s that can be obtained after exactly one move.

 

Examples

  • Input
    5
    1 0 0 1 0
  • Output
    4

 

  • Input
    4
    1 0 0 1
  • Output
    4

Note

  1. In the first case, flip the segment from 2 to 5 (i = 2, j = 5). That flip changes the sequence, it becomes: [1 1 1 0 1]. So, it contains four ones. There is no way to make the whole sequence equal to [1 1 1 1 1].
  2. In the second case, flipping only the second and the third element (i = 2, j = 3) will turn all numbers into 1.

 

Question 3

Rahul has an array a, consisting of n integers a1, a2, …, an. The boy cannot sit and do nothing, he decided to study an array. Rahul took a piece of paper and wrote out m integers l1, l2, …, lm (1 ≤ li ≤ n). For each number li he wants to know how many distinct numbers are staying on the positions li, li + 1, …, n. Formally, he want to find the number of distinct numbers among ali, ali + 1, …, an.?

Rahul wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.

Input

  • The first line contains two integers n and m (1 ≤ n, m ≤ 105). The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 105) — the array elements.
  • Next m lines contain integers l1, l2, …, lm. The i-th line contains integer li (1 ≤ li ≤ n).

Output

  • Print m lines — on the i-th line print the answer to the number li.

Examples

  • Input
    10 10
    1 2 3 4 1 2 3 4 100000 99999
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
  • Output
    6
    6
    6
    6
    6
    5
    4
    3
    2
    1

 

Question 4

Street Lights are installed at every position along a 1-D road of length n.

Locations[] (an array) represents the coverage limit of these lights. The ith light has a coverage limit of locations[i] that can range from the position max((i – locations[i]), 1) to min((i + locations[i]), n ) (Closed intervals). Initially all the lights are switched off. Find the minimum number of fountains that must be switched on to cover the road.

Example

n = 3

locations[] = {0, 2, 13}then

 

For position 1: locations[1] = 0, max((1 – 0),

1) to mini (1+0), 3) gives range = 1 to 1

For position 2: locations[2] = 2, max((2-2),

1) to min( (2+2), 3) gives range = 1 to 3

For position 3: locations[3] = 1, max( (3-1),

1) to min( (3+1), 3) gives range = 2 to 3



For the entire length of this road to be covered, only the light at position 2 needs to be activated.

 

Function Description

 

Returns

int: the minimum number of street lights that must be activated

 

Constraints

  • 1<_n<_ 10^5
  •  O<_locations[i] <_ mini (n,100) (where 1 <_1<_

10^5)

 

► Input Format For Custom Testing

 

Sample Case 0

 

Sample Input For Custom Testing

 

3 ->locations[] size n = 3

1 ->locations[] [1, 1, 1]

1 ->Sample Output

 

Sample Output

1

 

Question 5

You just received another bill which you cannot pay because you lack the money.

Unfortunately, this is not the first time to happen, and now you decide to investigate the cause of your constant monetary shortness. The reason is quite obvious: the lion’s share of your money routinely disappears at the entrance of party localities.

You make up your mind to solve the problem where it arises, namely at the parties themselves. You introduce a limit for your party budget and try to have the most possible fun with regard to this limit.

You inquire beforehand about the entrance fee to each party and estimate how much fun you might have there. The list is readily compiled, but how do you actually pick the parties that give you the most fun and do not exceed your budget?

Write a program which finds this optimal set of parties that offer the most fun. Keep in mind that your budget need not necessarily be reached exactly. Achieve the highest possible fun level, and do not spend more money than is absolutely necessary.

Input

  • The first line of the input specifies your party budget and the number n of parties.
  • The following n lines contain two numbers each. The first number indicates the entrance fee of each party. Parties cost between 5 and 25 francs. The second number indicates the amount of fun of each party, given as an integer number ranging from 0 to 10.
  • The budget will not exceed 500 and there will be at most 100 parties. All numbers are separated by a single space.
  • There are many test cases. Input ends with 0 0.

Output

  • For each test case your program must output the sum of the entrance fees and the sum of all fun values of an optimal solution. Both numbers must be separated by a single space.

Example

  • Sample input:
    50 10
    12 3
    5 8
    16 9
    16 6
    10 2
    21 9
    18 4
    12 4
    17 8
    18 9
    50 10
    13 8
    19 10
    16 8
    12 9
    10 2
    12 8
    13 5
    15 5
    11 7
    16 2
    0 0
  • Sample output:
    50 29
    48 32

Question 6

Raman was playing a game,  he starts with x coins. Now in every step, he wins and loses and he has to get the money or pay the money as needed. He came in contact with a psychic who can see the future and the Psychic predicted the outcomes after each step. Now Raman wants to start the game with the minimum wage where he doesn’t run out of money.  Help Raman to find what money he should start with. The only rule to keep playing is not going in a credit situation.

Input Format:

  • First line with n, number of steps in the game
  • Next n lines, n integers denoting outcomes of every game. Positive means winning and negative means losing that money.

Output Format:

  • One single integer denoting the minimum amount to start with

Constraints:

  • Number of steps<=10^9
  • -1000<=Money needed in each step<=1000

Sample Input:

4
2
-9
15
2

Sample Output:

7

Explanation:

If he starts with 7 rupees, then after steps : 7 ->9 -> 0-> 15 -> 17.

Question 7

Problem statement: Rocky is a software engineer and he is creating his own operating system called “myFirst os”. myFirst os is a GUI (Graphical user interface) based operating system where everything is stored in files and folders. He is facing issues on  creating unique folder names for the operating system . Help rocky to create the unique folder name for it’s os.If folder name already exists in the system and integer number is added at the name to make it unique. The integer added starts with 1 and is incremented by 1 for each new request of an existing folder name. Given a list of folder names , process all requests and return an array of corresponding folder names.

Example 

  • n=5
  • foldername= [‘home’ , ‘myfirst’ ,’downloads’, ‘myfirst’, ‘myfirst’]
  • Foldername[0] = ‘home’ is unique.
  • Foldername[1] = ‘myfirst’ is unique.
  • foldername [2] =’downloads’ is unique.
  • Foldername[3] =’myfirst’ already exists in our system. So Add1 at the end of the folder name i.e foldername[3] =”myfirst1″
  • Foldername[4 ]=’myfirst’ also already exists in our system.So add 2 at the end of the folder name i.e. foldername[4]=”myfirst2″.

Function description 

  • Complete the function folderNameSystem In the editor below
  • folderNameSystem has the following parameters
  • string foldername[n]: an array of folder name string in the order requested

Returns:

  • String[n]:  an array of strings usernames in the order assigned

Constraints

  •     1<=n<=10^4
  •     1<=length of foldername[i]<20
  •     foldername[i] contains only lowercase english letter in the range ascii[a-z]

Input Format:

  • The first line contains an integer n , denoting the size of the array usernames Each line i of the n subsequent lines (where i<=0<=n) contains a string usernames[i] representing a username request in the order received.

Sample case 0

  • 4
  • home 
  • download
  • first
  • first

Sample Output 0

  • home
  • download
  • first
  • first1

Explanation 0

  •    foldername[0] = ‘home’ is unique
  •    foldername[1]=’download’ is unique
  •    foldername[2]= ‘first’ is unique
  •    foldername[3]=’first’ is already existing . so add 1 to it and it become first1

Question 8 :

Problem Statement :

Rahul is playing a game, wherein he has multiple coloured wooden blocks, stacked one above the other, his task is to remove all the wooden blocks from the stack, without letting it fall and in the minimum number of steps. He can remove one block of a colour at a time, but he can remove multiple blocks of the same colour together. Determine the minimum number of steps in which he can perform this task.

For example, if you remove [red,red] from (white,red,red,white), the resulting array is [white,white].

Note- there are only two colour blocks – red and white

Function description :

Complete the minMoves function in the provided editor. It contains the following parameters:

Parameters:

NameTypeDescription
NIntegerNo. of Wooden blocks
Array[ ]Integer ArrayArray of Blocks.

Input format :

The first line contains an integer n denoting the number of blocks. Each n line denotes the colour of the wooden block .

Constraints :
1<=n<=700
0<=a[i]<=1

Sample input 1 :

4
red
white
white
red

Sample Output 2 :

2

Explanation :

Remove [white,white] first The array will be [red,red] The remaining numbers can  be removed in one strap .

Sample Input 1:

4
white
red
white
red

Sample Output 1:

3

Sample Explanation:
0
The steps are [white,red,white,red]->[red,white,red]->[red,red]->[]. Therefore the answer is 3.

Question 9

Problem Statement  :

You’re given a string, you’ve to print additional characters needed to make that string a palindrome.

A Palindrome is a sequence of characters that has the property of reading the same in either direction.

Input :
abede
Output :
ba

Sample Input :
abcfe

Sample output :
fcba

Question 10

Problem Statement  :

Semester exams are going on for university students. Examiners noticed that a group of people are trying to cheat. They marked students of that group as ‘1’ and students of another group ( who are not cheating ) as ‘0’ 

We can reduce cheating by not allowing students from group 1 to sit together, means no two students from group 1 can sit together. Seatings are marked using above conditions. Your task is to give the seating placement of nth possibility Possibility order from 1 to 10 is given below

[1  10  100  101  1000  1001  1010  10000  10001  10010]

Sample input :

3 → number of test cases

4

6

9

Sample output :

101

1001

10001

Explanation :

4th possibility is 101 

6th possibility is 1001

9th possibility is 10001

Disclaimer:

The following are not the actual questions of Goldman Sachs, these are just practice questions created by the PrepInsta team
internally and are the original creation of PrepInsta, created in fair use as per the Govt of India fair use.

Get PrepInsta Prime Mock  Course

Get finely crafted PrepInsta Prime Subscription to ease your preparation!

Analyze your prepration with PrepInsta Prime Mock

100+ Topic-Wise Mocks

  • All Mocks are based on Goldman Sachs Previous Year Papers
  • Detailed analytics and Smart Dashboard