Q) Given a string consisting of only 0, 1, A, B, C where
A = AND
B = OR
C = XOR
Calculate the value of the string assuming no order of precedence and evaluation is done from left to
right.
Constraints – The length of string will be odd. It will always be a valid string.
Example, 1AA0 will not be given as an input.
Examples:
Input: 1A0B1
Output : 1 1 AND 0 OR 1 = 1 Input : 1C1B1B0A0 Output : 0
Java Program
// Java program to evaluate value of an expression.
class Main
// Evaluates boolean expression
// and returns the result
static int evaluateBoolExpr (StringBuffer s)
int n = [Link] ();
// Traverse all operands by jumping
// a character after every iteration.
for (int i = 0; i < n; i += 2)
// If operator next to current operand
// is AND.
if (i + 1 < n && i + 2 < n)
if ([Link] (i + 1) == 'A')
{
if ([Link] (i + 2) == '0'||[Link] (i) == 0)
[Link] (i + 2, '0');
else
[Link] (i + 2, '1');
// If operator next to current operand
// is OR.
else if ((i + 1) < n && [Link] (i + 1) == 'B')
if ([Link] (i + 2) == '1'||[Link] (i) == '1')
[Link] (i + 2, '1');
else
[Link] (i + 2, '0');
// If operator next to current operand
// is XOR (Assuming a valid input)
else
if ([Link] (i + 2) == [Link] (i))
[Link] (i + 2, '0');
else
[Link] (i + 2, '1');
return [Link] (n - 1) - '0';
// Driver code
public static void main (String[]args)
String s = "1C1B1B0A0";
StringBuffer sb = new StringBuffer (s);
[Link] (evaluateBoolExpr (sb));
Python Program
def solve(s):
s=[Link]("A","&").replace("B","|").replace("C","^")
return eval(s)
s=input()
print(solve(s))
Q) Make a function which accepts a string as an argument that may contain repetitive characters.
Implement the function to modify and return the input string, such that each character once, along
with the count of consecutive occurrence. Do not append count if the character occurs only once.
Programming
Note –
The string will only contain lowercase English Alphabets
If you have to manipulate the input string in place you cant use another string
Assumption –
No character will occur consecutively more than 9 times.
Example –
Input
aaaaabbbccccccccdaa
Output
a4b3c8da2
Java Program
import [Link];
public class Main
public static void main (String[]args)
{
try (Scanner sc = new Scanner ([Link]);
StringBuilder sb = new StringBuilder ([Link] ());
int count = 1;
char current = [Link] (0), next;
for (int i = 1; i < [Link] (); i++)
next = [Link] (i);
if (next == current)
[Link] (i);
i = i - 1;
count++;
else
[Link] (i, count);
count = 1;
current = next;
i = i + 1;
[Link] (count);
[Link] (sb);
Python Program
def solve(s):
ans=""
c=1
for i in range(len(s)-1):
if(s[i]==s[i+1]):
c+=1
else:
if(c==1):
ans+=s[i]
else:
ans+=s[i]+str(c)
c=1
if(c==1):
ans+=s[i+1]
else:
ans+=s[i+1]+str(c)
return ans
s=input()
print(solve(s))
Q) Write a function which accepts a string str, implement the function to find and return the
minimum characters required to append at the end of str to make it a palindrome
Assumptions –
The string will only contain lowercase English Alphabets
Note –
If string is already a palindrome then return NULL
You have to find the minimum characters required to append at the end of the string to
make it a palindrome
Example –
Input –
abcdc
Output –
ba
Java Program
import [Link];
public class Main
public static void main (String[]args)
try (Scanner sc = new Scanner ([Link]);
StringBuilder sb = new StringBuilder ([Link] ());
StringBuilder s = new StringBuilder ();
Boolean finish = true;
while (finish)
if (isPalindrome (sb))
[Link]("NULL");
finish = false;
else
[Link] ([Link] (0));
sb = new StringBuilder ([Link] (1));
[Link] ([Link] ());
public static Boolean isPalindrome (StringBuilder sb)
{
int l = [Link] () / 2;
int m = [Link] () - 1;
for (int i = 0; i <= l; i++)
if (!([Link] (i) == [Link] (m - i)))
return false;
return true;
Python Program
def ispalindrome(s):
return s==s[::-1]
def solve(s):
if(ispalindrome(s)):
return None
for i in range(len(s)):
x=s[:i][::-1]
if(ispalindrome(s+x)):
return x
s=input()
print(solve(s))
Q) Write a function which returns an integer based on some conditions. You were given with two
integers as input say n and m
if n>m return (n*m)-(n-m)
if n<=m return (m%n)-(m+n)
Example:
Sample input:
n=10
m=18
Sample output:
-20
Explanation:
m%n=18%10=8
m+n=28
answer= 8-28=-20
Java Program
import [Link];
import [Link].*;
class Main {
public static void main (String[] args) {
Scanner prep = new Scanner( [Link] );
int n,m;
n= [Link]();
m= [Link]();
[Link](solve(n,m));
public static int solve(int n, int m)
if(n>m)
return (n*m)-(n-m);
else
return (m%n)-(m+n);
Python Program
def solve(n,m):
if(n>m):
return (n*m)-(n-m)
else:
return (m%n)-(m+n)
n=int(input())
m=int(input())
print(solve(n,m))
Q) Write a function which returns the sum of elements whose frequency in the array is odd. Means
find sum of elements whose Number of occurrences is odd
Example:
Input:
15
arr=[1,1,2,2,2,3,4,4,5,5,5,5,6,7,7]
Output:
11
Explanation:
count of each element is as follows-
1–>2, 2–>3, 3–>1, 4–>2, 5–>4, 6–>1, 7–>2
Odd number of time occured elements are, 2,3,6 and its sum if 11
Python Program
def solve(arr,n):
count=0
for i in set(arr):
if([Link](i)%2==1):
count+=i
return count
n=int(input())
arr=list(map(int,input().split()))
print(solve(arr,n))
Q) Write a function to return the count of alphanumeric characters in a given string.(Count
number of alphabets and numerics in a string)
Example :
Input:
Hello World!123
Output:
13
Java Program
import [Link];
import [Link].*;
class Main {
public static void main (String[] args) {
[Link](solve("Hello World!123"));
public static int solve(String str)
int count= 0;
char[] str1 = [Link]();
for (int i = 0;i< [Link]; i++)
if ((str1[i] >= 'A' && str1[i] <= 'Z')|| (str1[i] >= 'a' && str1[i] <= 'z') )
count++;
if(str1[i] >= '0' && str1[i] <= '9')
count++;
return count;
Python Program
def solve(str):
count=0
for i in str:
if([Link]() or [Link]()):
count+=1
return count
n=input()
print(solve(n))