# WeCP Coding Questions

## WeCP Practice Coding Questions for Freshers

WeCP is a technical skill assessment platform which helps the organization in recruiting students who are highly proficient in technical skills like hardware and software. One of the most important sections of WeCP exams is the coding round. ## WeCP Practice Coding Questions for Freshers

WeCP is a technical skill assessment platform which helps the organization in recruiting students who are highly proficient in technical skills like hardware and software. One of the most important sections of WeCP exams is the coding round. ## More About WeCP Companies based Coding Questions

WeCP Coding questions are not that tough but they are tricky, and because of the time constraints it makes it tough to solve them. Here, we have provided a few sample questions based on the pattern of questions that companies asks through WeCP. These questions will help you in preparing for the exams, make sure you go through all of them.

### Pattern for WeCP Companies based Coding Questions

Coding RoundDetails
Number of Questions2-3
Time Limit2 hrs(approx)
Difficulty levelmoderate
Package Offered5 LPA – 7.5 LPA

## Practice Questions for WeCP Coding Round

### Question 1: Sentence counter

Problem statement-:  We know sentences are terminated with certain punctuations like ‘.’,’!’,’?’, whereas there are several punctuations which don’t terminate the punctuations. Given for a paragraph, write a code to find out how many sentences are there.

Note that, ‘…’,’,’,’-‘ these don’t terminate a sentence.

Constraints:

• Number of words in the paragraph<=10^8

Input Format:

• Paragraph ended with an enter.

Output Format:

• Single Integer denoting number of sentences.

Sample Input:

Hello! How are you? Last time I saw you… you were nervous.

Sample Output:

3

Explanation:

The three sentences are:

hello!

How are you?

Last time I saw you… you were nervous.

`#include <bits/stdc++.h>using namespace std; int SentenceCount(string s){    istringstream ss(s);    int ans=0;    while(ss)    {        string w;ss>>w;        if(w=="") break;        if(w[w.length()-1]=='!'||w[w.length()-1]=='?') ans++;        else if(w==".") ans++;        else if(w.length()>1)        if(w[w.length()-1]=='.'&&w[w.length()-2]!='.') ans++;    }    return ans;} int main(){    string s;    getline(cin,s);    cout<<SentenceCount(s);}`
`def SentenceCount(L):    ans=0    for w in L:        if w[len(w)-1]=='!' or w[len(w)-1]=='?':            ans+=1        elif(w=="."):            ans+=1        elif len(w)>1:            if w[len(w)-1]=='.' and w[len(w)-2]!='.':                ans+=1     return ans                def main():    L=list(map(str,input().split()))        result = SentenceCount(L);    print(result)if __name__ == "__main__":    main()`
`import java.util.Scanner;public class Application12 {	public static void main(String[] args) {		Scanner sc = new Scanner(System.in);		String s = sc.nextLine();		System.out.println(sentenceCount(s));	}	private static int sentenceCount(String s) {		String[] w = s.trim().split("\\s+");		int ans=0;		for(int i=0;i<w.length;i++) {			String k = w[i];			int len = k.length();			if(k.lastIndexOf('!')==(len-1) || k.lastIndexOf('?')==(len-1) ) {				ans++;			}else if(k.equals(".")) {				ans++;			}else if(len>1) {				if(k.charAt(len-1)=='.'&&k.charAt(len-2)=='.') {					ans++;				}			}else {				}		}		return ans;	}}`

### Question 2: Even Odd Even Odd

Problem Statement -:  A number is even odd special if and only if there are even digits side by side, same goes for odd digits too. So the distributions must be in an alternating basis of even digits and odd digits. You are given a number N, and you have to say how many special numbers are possible <=N, and are greater than 0.

Constraints:

• 1<=N<=10^8

Input Format:

• First Line containing a single integer N.

Output Format:

• Single line containing a single integer denoting the possible number of special numbers.

Sample Input:

30

Output:

20

Explanation:

20 such special numbers are there :-

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 21, 23, 25, 27, 29, 30

`#include<bits/stdc++.h>using namespace std;int ans;int a,l;void func(string s){    int S=stoi(s);    if(a>=S)    {        cout<<s<<endl;    ans++;}    if(s.length()==l) {     return;    }    if ((S%10)&1)    for(int i=0;i<9;i+=2)    func(s+to_string(i));    else    for(int i=1;i<=9;i+=2)    func(s+to_string(i));} int main(){    string s;    cin>>s;ans=0;    l=s.length();     a=stoi(s);    for(int i=2;i<9;i+=2)    func(to_string(i));    for(int i=1;i<=9;i+=2)    func(to_string(i));    cout<<ans;}`
`a=input()ans=0l=len(a)def func(s):    global ans    global a    global l    S=int(s)    if int(a)>=S:        ans+=1     if len(s)==l:        return    if (S&1):        for i in range(0,9,2):            func(s+str(i))    else:        for i in range(1,10,2):            func(s+str(i))            for i in range(2,9,2):    func(str(i))for i in range(1,10,2):    func(str(i))print(ans)`

### Question 3: Profit balance

Problem Statement-: Anand and Brijesh got a bunch of profit in their business. Now it’s time to divide the profit between themselves. Anand being the one with the calculator was the one who could decide who will get how much. But as he was one hell of an honest guy, he can’t cheat on Brijesh to get some extra money. So he decided to divide them in such a way where they can get almost the same amount possible. Although, it is sometimes impossible to divide the profit into two, because the profits must be written in their production managed copy in a way where you cannot share the profit of a single thing, A single Profit will be held by a single person.

You are the one who is called to mitigate the problem. Given an array of profit, find out in the process, what can be the minimum difference between them in terms of income.

Constraints:

• 1<=N<=10^3
• 0<=Profits<=1000

Input Format:

• First line contains an integer N, number of profits.
• Next line, N space separated Integers denoting the i-th Profit.

Output:

• A single integer denoting minimum possible difference between the total profits.

Sample Input:

4

1 2 3 4

Output:

0

Explanation:

He will take 1 and 4, so his profit will be 5, so the difference will be 0.

`#include<bits/stdc++.h>using namespace std;vector<int> arr;int n,s;map<int,map<int,int>> m; int MinDif(int i,int ss){    if(m[i][ss]) return m[i][ss];    if(i==n) return m[i][ss]= abs(s-2*ss);    if(ss>=s/2) return m[i][ss]=abs(s-2*ss);    return m[i][ss]=min(MinDif(i+1,ss+arr[i]),MinDif(i+1,ss));} int main(){    cin>>n;int a;    s=0;    for(int i=0;i<n;i++)    {cin>>a;arr.push_back(a);s+=a;}    cout<<MinDif(0,0);}`
`from collections import defaultdictm = defaultdict(lambda: defaultdict(int))def MinDif(i,ss,a,n,m):    global s    if m[i][ss] != 0:        return m[i][ss]    if i==n:        m[i][ss]=abs(s-(2*ss))        return m[i][ss]    if ss>=s//2:        m[i][ss]=abs(s-(2*ss))        return m[i][ss]    m[i][ss]=min(MinDif(i+1,ss+a[i],a,n,m),MinDif(i+1,ss,a,n,m))    return m[i][ss]n=int(input())a=list(map(int,input().split()))s=sum(a)print(MinDif(0,0,a,n,m))`
`import java.util.HashMap;import java.util.Scanner;public class Application14 {	public static void main(String[] args) {		Scanner sc = new Scanner(System.in);		int n = sc.nextInt();		int s=0;		int arr[]=new int[n];		for(int i=0;i<n;i++) {			arr[i] = sc.nextInt();			s+=arr[i];		}		HashMap<Integer,HashMap<Integer,Integer>> m = new HashMap<>();		System.out.println(minDiff(0,0,s,n,m,arr));	}	private static int minDiff(int i, int j, int s, int n, HashMap<Integer, HashMap<Integer, Integer>> m, int[] arr) {		if(m.containsKey(i)) {			if(m.get(i).containsKey(j))			return m.get(i).get(j);		}		if(i==n) {			HashMap<Integer,Integer> z = new HashMap<>();			z.put(j,  Math.abs(s-2*j));			m.put(i,z);			return m.get(i).get(j);		}		if(j>=s/2) {			HashMap<Integer,Integer> z = new HashMap<>();			z.put(j,  Math.abs(s-2*j));			m.put(i,z);			return m.get(i).get(j);		}		int temp = Math.min(minDiff(i+1,j+arr[i],s,n,m,arr), minDiff(i+1,j,s,n,m,arr));		HashMap<Integer,Integer> z = new HashMap<>();		z.put(j, temp);		m.put(i, z);		return m.get(i).get(j);	}}`

### Question 4: Class Monitor

Problem Statement-: After JEE Mains, some students got admission into an engineering college. Now there is a class consisting of such n students, and the HOD came to say it is time to select the class monitor. But He never gets all of them at one time. So he brought a register, every time he gets someone with less rank than the previous time he cut the name and wrote the name of the student and the rank.

For a given number of ranks he gets each time, you have to predict how many names are cut in the list.

Constraints:

• Number of Visiting<=10^9
• ranks <=10000

Input Format:

• Number of Visiting N in their first line
• N space separated ranks the HOD gets each time

Output Format:

• Number of ranks cut in the list

Sample Input:

6

4 3 7 2 6 1

Sample Output:

3

`#include <bits/stdc++.h>using namespace std; int main(){    int n,p,m=INT_MAX,ans=0;    cin>>n;    vector<int> a(n);    for(int i=0;i<n;i++)    {        cin>>p;        if(p<m) {m=p;ans++;}    }    cout<<ans-1;}`
`n=int(input())a=list(map(int,input().split()))m=aans=0for i in range(1,n):    if a[i]<m:        m=a[i]        ans+=1print(ans-1)`
`import java.util.Scanner;public class Application15 { public static void main(String[] args) {  int n,p,ans=0,m=Integer.MAX_VALUE;  Scanner sc = new Scanner(System.in);  n = sc.nextInt();  for(int i=0;i<n;i++) {   p = sc.nextInt();   if(p<m) {    m=p;    ans++;   }  }  System.out.print(ans-1); }}`

### Question 5: Corona for Computer

Problem Statement-: Every decimal number can be changed into its binary form. Suppose your computer has it’s own CoronaVirus, that eats binary digits from the right side of a number. Suppose a virus has 6 spikes, it will eat up 6 LSB binary digits in your numbers.

You will have a bunch of numbers, and your machine will have a virus with n spikes, you have to calculate what will be the final situation of the final numbers.

Input Format:

• First line, a single Integer N
• Second line N space separated integers of the bunch of values as array V
• Third line a single integer n, the number of spikes in Corona for Computer

Output Format:

• Single N space separated integers denoting the final situation with the array v.

Sample Input:

5

1 2 3 4 5

2

Sample Output:

0 0 0 1 1

Explanation:

5 is 101 in binary, when you cut the last two binary digits, its 1.

`#include <bits/stdc++.h>using namespace std; int main(){    int N,n;cin>>N;    vector<int> v(N);    for(int i=0;i<N;i++) cin>>v[i];    cin>>n;    for(auto i:v)    cout<<(i>>n)<<" ";}`
`N=int(input())a=list(map(int,input().split()))n=int(input())s=""for i in a:    s+=str(i>>n)+" "print(s)`
`import java.util.Scanner;public class Application16 { public static void main(String[] args) {  Scanner sc = new Scanner(System.in);  int n = sc.nextInt();  int arr[]=new int[n];  for(int i=0;i<n;i++) {   arr[i] = sc.nextInt();  }  int k = sc.nextInt();  for(int i:arr) {   System.out.print((i>>k));   System.out.print(" ");  } }}`

### Question 6: Help of Prepsters

Problem Statement-: Arnab has given me a challenge. I have to calculate the number of numbers which are less than a certain value n, and have exactly k set bits in its binary form. As you are a Prepster like me, help me write a code that will take input for n and k and give the expected output.

Constraints :

• 1<=n<=10000
• 1<=k<=10

Input Format :

• First line containing n and k space separated

Output Format :

• Number of numbers present in a single line

Sample Input:

7 2

Sample Output:

3

Explanation:

011,110,101 -> These three numbers.

`#include <bits/stdc++.h>using namespace std;int n,k,ans,l;map<int,int> m; void func(int i,string s,int L){   // cout<<s<<endl;    if(L>l) {return;}    if(i==0 && m[stoull(s,0,2)]==0) {m[stoull(s,0,2)]++;ans++;}    if(s!="") if(n<stoull(s,0,2)) {return;}    func(i-1,s+"1",L+1);    func(i,s+"0",L+1);} int main(){    cin>>n>>k;char res;itoa(n,res,2);    ans=0;l=strlen(res);    func(k-1,"1",1);    cout<<ans;}`
`ans=0n,k=map(int,input().split())l=len(bin(n)[2:])def func(i,s,L):    global l     global ans    if(L>l):        return    if i==0 :        ans+=1     if s!="":        if n<int(s,2):            return    func(i-1,s+"1",L+1)    func(i,s+"0",L+1)    func(k-1,"1",1)print(ans)`
`import java.util.Scanner;public class Application17 {	public static void main(String[] args) {		Scanner sc = new Scanner(System.in);		int n = sc.nextInt();		int k = sc.nextInt();		int ans=0;		for(int i=1;i<n;i++) {			if(countSetBits(i)==k) {				ans++;			}		}		System.out.println(ans);	}	private static int countSetBits(int i) {		if(i==0)		return 0;		else			return 1 + countSetBits(i&(i-1));	}}`

Problem Statement -:  Ratul made a linked list, a list made of n nodes, where every node has two variables, the velocity and the mass of a particle.

Since all the particles have the velocity in the same direction, find the total momentum of the entity made by the particles from the linked list.

Constraints :

• 1<=n<=10000
• 1<=m,v<=100

Input format:

• First line containing n, number of nodes
• Then n lines containing the mass and the velocity space separated.

Output Format:

• Single integer denoting the momentum

Sample Input:

4

1 3

2 4

2 3

4 5

Sample Output:

37

`#include <bits/stdc++.h>using namespace std; int main(){    int n,s=0,m,v;    cin>>n;    for(int i=0;i<n;i++)    {        cin>>m>>v;        s+=(m*v);    }    cout<<s;}`
`n=int(input())s=0for i in range(n):    m,v=map(int,input().split())    s+=(m*v)print(s)`
`import java.util.Scanner;public class Application18 { public static void main(String[] args) {  Scanner sc = new Scanner(System.in);  int n = sc.nextInt();  int s = 0;  for(int i=0;i<n;i++) {   int m=sc.nextInt();   int v= sc.nextInt();   s+=(m*v);  }  System.out.println(s); }}`

### Question 8: Lazy String

Problem Statement-:  Anish is the laziest person you can ever see. He is tasked to write the name of the winner in a game where two people take part. And he just writes the longest common subsequence over there, so that with minimum chane or no backspace he can edit the name to the winner’s name.

For two given names, you have to predict what Anish will write in his computer before the start of the name. If there are more than two longest subsequences possible,write the one with less lexicographic value.

Input Format:

• Two lines including two strings of name(All with capital letters)

Output Format:

• A single line with the lexicographically smallest possible longest common subsequence.

Sample Input:

ABCD
BACD

Sample Output:

ACD

Explanation:

ACD and BCD these are the two possible biggest substring

`#include <bits/stdc++.h>using namespace std;string s1,s2,s3; int fun(int i,int j,int k,string s){    if(i==s1.length()||j==s2.length())    {        if(s3.length()==s.length()) s3=min(s,s3);        else if(s3.length()<s.length()) s3=s;        return k;    }    if(s1[i]==s2[j]) return fun(i+1,j+1,k+1,s+s1[i]);    return max(fun(i+1,j,k,s),fun(i,j+1,k,s));} int main(){    cin>>s1>>s2;    fun(0,0,0,"");    cout<<s3;}`
`s1=input()s2=input()s3=""def fun(i,j,k,s):    global s1    global s2    global s3    if i==len(s1) or j==len(s2):        if len(s3)==len(s):            s3=min(s,s3)        elif len(s3)<len(s):            s3=s        return k     if s1[i]==s2[j]:        return fun(i+1,j+1,k+1,s+s1[i])    return max(fun(i+1,j,k,s),fun(i,j+1,k,s))    fun(0,0,0,"")print(s3)`
`import java.util.Scanner;public class Application19 {	public static String s1,s2,s3="";	public static void main(String[] args) {		Scanner sc = new Scanner(System.in);		 s1 = sc.nextLine();		 s2 = sc.nextLine();		 fun(0,0,0,"");		 System.out.println(s3);	}	private static int fun(int i, int j, int k, String s) {		// TODO Auto-generated method stub		if(i==s1.length() || j==s2.length()) {			if(s3.length()==s.length()) {				if(s3.compareTo(s)>0) {					s3=s;				}			}			else if(s3.length()<s.length()) {				s3=s;			}			return k;			}		if(s1.charAt(i)==s2.charAt(j)) {			return fun(i+1,j+1,k+1,s+s1.charAt(i));		}		return Math.max(fun(i+1,j,k,s), fun(i,j+1,k,s));	}}`