Salesforce Coding Questions

In Salesforce there are 3 rounds, first one was technical test after that Technical interview and HR Interview.

Possible Playlists

Guru has a music player loaded with N different songs he wants to listen to L (not necessarily different) songs during his travel to his hometown.
Create a playlist so that:
Every song is played at least once
A song be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 109+7.
Sample Test Cases Input Format:
Provide the values of the variables N,L,K in newlines in the respective order. Input: N = 3, L=3, K=1, Provide as follows


3
3
1
Output: 6
Explanation: There are 6 possible playlists. [1,2,3] , [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]
Input: N=2, L=3 , K=0. Provide these inputs as follows

2
3
0
Output: 6 Explanation: There are 6 possible playlists. [1,1,2], [1,2,1], [2,1,1], [2,1,1], [2,2,1], [2,1,2], [1,2,2]

    
        def possiblePlaylists(N, L, K):
        Max = 10**9 + 7
        dp = [1 for i in range(L-N+1)]
    
        for p in range(2,N-K+1):
            for i in range(1,L-N+1):
                dp[i] += dp[i-1]*p
                dp[i] %= Max
    
        ans = dp[L-N]
    
        for k in range(2,N+1):
            ans = ans*k % Max
    
        return ans//1

    

Balls in Basket

Given an array of balls of different colors, there are two baskets and the goal is to put the maximum number of balls in each basket. The only restriction is that each basket can have one or more balls of only one color.
You can start with any ball, but once you have started you cannot skip a ball in the array. You will pick one ball from each color until you cannot (i.e. you will stop when you have to pick from a third color type).
Write a function to return the maximum number of balls in both baskets.
Input Format:
Number of Balls
<Followed by balls each per line> ========================== Example: Input:


5
RED
GREEN
BLUE
RED
BLUE
Output: 3
Explanation: Put 2 ‘BLUE’ balls in one basket and one ‘RED’ ball in the other basket.

    
        def balls_into_baskets(balls):
        n = len(balls)
        dp = [0 for i in range(n)]
        dp[0] = 1
        if n==1:
            return 1
        freq = {}
        for b in balls:
            if freq.get(b):
                pass
            else:
                freq[b] = 0
        diff = 1
        freq[balls[0]] = 1
        left = 0
        right = 1
        while (right<n):
            freq[balls[right]] += 1
            if(freq[balls[right]])==1:
                diff += 1
            if diff>2:
                while diff>2:
                    freq[balls[left]] -= 1
                    if (freq[balls[left]]) == 0:
                        diff -= 1
                dp[right] = right - left + 1
            else:
                dp[right] = right - left + 1
            right += 1
        maxx = max(dp)
        return maxx

    

Just Another Board Game

Joe is thinking of designing a game for 2 people. He needs your help for the same. The game will be played as follows.
There is a Board of size N, where N>=6,, and N is an even number. This will translate into an N*N Matrix.
Each player starts from position (1,1).
Each player will input String consisting of a series of numbers separated by a space.
Player 1 has to try to input numbers between 1 and N*N in the odd binary sequences( a number that has an odd number of 1s in its binary expansion)
Player 2 has to try to input numbers between 1 and N*N in the even binary sequence(a number that has an even number of 1s in its binary expansion)
The system should evaluate each number entered by players separately and move their positions forward/back based on the following conditions:
If a player states the number correctly they move forward by three positions.
If a player states the number incorrectly they move back by one position, exception being if they are at position (1,1).
If a player enters a perfect number within their sequence(number is represented in binary form as p ones followed by p-1 zeros), they move forward by six positions.
The player cannot reuse a number that they entered before if used again, it should count as an incorrect move(move back one position).
The game ends in the following scenarios.
Any player that reaches the position (N,N) or beyond in fewer moves. That player will be declared the winner.
Keep in mind that each player should have played an equal number of times. So if both Players reach Position (N,N) at the same time, declare the game as a draw.
If both players have exhausted their moves, then the Player farthest ahead will be declared the winner. If both players are at the same position, declare the game as a draw.
Input:
The first line will accept an Integer N for Board size where N>=6 , N%2==0
The second line will accept an String consisting of numbers separated by a space for Player1’s moves.
The third line will accept an String consisting of numbers separated by a space for Player2’s moves.
Output: Print integer “1”, if Player1 wins, Print integer “2” if Player2 wins, Print “0” if the game is a draw.
Example:
I.)
Input:


6
1 2 28 9 11 13 15 17 18 19 21 22 24 25 26 27 31 36
2 4 6 8 5 12 13 14 10 16 20 23 28 29 30 32 33 35
Output:

1
Explanation:
Considering Player1’s moves first
1 => 00000001 => valid sequence => new position = 1,4
3 => 00000011 => invalid sequence => new position = 1,3
28 => 00011100 => valid sequence and perfect number => new position = 2,3
checking the remaining number we get Player1’s final position as 5,2

Considering Player2’s moves
2 => 00000010 => invalid sequence => new position = 1,1
4 => 00000100 => invalid sequence => new position = 1,1
6 => 00000110 => valid sequence and perfect number => new position = 2,1
checking the remaining number we get Player2’s final position as 4,6
As Player1 is farthest than Player2, the system outputs 1 indicating the Player1 is the winner.

II.)
Input:

6
1 2 4 7 8 11 13 14 16 19 21 22 25 26 28 31 32 35
3 5 6 9 10 12 15 17 18 20 23 24 27 29 30 33 34 36
Output:

2
Explanation:
Based on the numbers entered, Player2 reaches N,N in the fewest moves. Hence the system outputs 2 indicating the Player2 is the winner.

    
        #include<bits/stdc++.h>
        using namespace std;
        
        //position
        struct position{
            int x,y;
        };
        
        //count no of binary 1s in the no
        int count1s(int x){
            int count=0;
            while(x!=0){
                int last_bit = x & 1;
                if(last_bit==1)
                  count++;
                x = x>>1;  
            }                
            return count;
        }
        
        //check for perfect no
        bool isPerfect(int x){
            bool isZero=false;
            int numZeros = 0;
            while(x!=0){
                int last_bit = x & 1;
               
                if(last_bit==0){
                   !numZeros?numZeros+=2:numZeros++;
                }else{
                    if(!numZeros)
                       break;
                    numZeros--;   
                }
                x = x>>1;             
            }
        
            if(x!=0)
              return false;
            else if(numZeros==0)  
              return true;
            return false;    
        
        }
        
        //check which player exceeded the limit of board
        bool isWinner(struct position pp,int n){
            if(pp.x>=n && pp.y>=n)
             return true;
            return false; 
        
        }
        
        //check which player has greater position
        bool isBeyond(struct position pp1,struct position pp2){
            if(pp1.x>pp2.x)
              return true;
            else if(pp1.x==pp2.x){
                if(pp1.y>pp2.y)
                 return true;
                return false; 
            }  
            return false;
        }
        
        //update postion (valid)
        void update_pos_forward(struct position &pp,int x, int n){
            int inc=-1;
            int mod = n+1;
            inc = isPerfect(x)?6:3;
            pp.x = pp.x + (pp.y + inc)/mod;
            pp.y = (pp.y + inc);
            if(!isWinner(pp,n)){
                pp.y = pp.y%mod;
                if(pp.y==0)
                    pp.y = 1; 
            }
        }
        
        //update position (invalid)
        void update_pos_backward(struct position &pp,int n){
        
            if(pp.x==1 && pp.y==1)
             return;
        
            pp.y = pp.y-1;
            if(pp.y==0){
                pp.x = pp.x-1;
                pp.y = n; 
            }
        
        }
        
        int find_winner(int n,vector p1,vector p2){
            int m = p1.size();
            struct position pp1;
            struct position pp2;
            pp1.x = pp2.x = 1;
            pp1.y = pp2.y = 1;
            int i=0;
            while(!isWinner(pp1,n) && !isWinner(pp1,n) && i<m){
        
                  //for player 1
                  if(count1s(p1[i])%2==1)
                     update_pos_forward(pp1,p1[i],n);
                  else
                     update_pos_backward(pp1,n);
        
                  //for player 2
                  if(count1s(p2[i])%2==0)
                     update_pos_forward(pp2,p2[i],n);
                  else
                      update_pos_backward(pp2,n);
        
                  i++;
            }
        
            if(isWinner(pp1,n) && !isWinner(pp2,n))
             return 1;
            else if(!isWinner(pp1,n) && isWinner(pp2,n))
               return 2;
            else{
                if(isBeyond(pp1,pp2))
                  return 1;
                else if(isBeyond(pp2,pp1))
                      return 2;    
                return 0;    
            } 
            
            return 0;  
        }
        
        
        int main(){
        
            int n; //size>=6
            cin>>n;
        
            int numMoves;
            cin>>numMoves;
            vector<int> p1(numMoves),p2(numMoves);
            for(int i=0;i<numMoves;i++)
             cin>>p1[i];
            for(int i=0;i<numMoves;i++)
             cin>>p2[i]; 
            cout<<find_winner(n,p1,p2)<<endl;;
            
            return 0;
        }