Persistent Automata Questions and Answers

Persistent_Coding_Questions

Persistent Automata Questions

Persistent has made some changes in its coding round this year. You will have 2-3 questions to solve within the duration of about 45 mins. You can code in languages like C, C++, Java, Python, Ruby, Perl etc.

How to prepare for Persistent Automata Test:

  • Always Remember there may be some hidden test cases.Try to cover and pass all the given test cases.
  • Try to give optimal solution for the given problem statements which may gain you additional benefit in the Automata Section as well as Technical Interview.
  • Try to Solve all the given problems in specific language instead of using different languages for different problems.
  • Read complete problem statement, then try to structure the optimal code and then go for the coding. Don’t code by reading the problem line by line.
  • Also, there is a time limit of 45 minutes.So be prepared with your time management skills.
Coding Section Details
No. of Questions 2 – 3
Time Duration 45 mins
Difficulty Level Moderate
We have given below some coding questions with solution to give you an overview. Please go through them while preparing for Persistent Coding Test.

Question-1

Ram is a 6 years old boy who loves to play with numeric legos. One day Ram’s mom created a number using those legos and asked ram to tell the number of elements available between two specific numbers ‘alpha1’ and ‘alpha2’. After 15 years when Ram started learning to program, he now wants to write a code to find the number of elements lies between ranges alpha1 and alpha2. If the number is arr and the starting and ending points are alpha1 and alpha2, find the numbers of elements lies in the range.

Input:

  • Three space-separated integers
    1. First is the length of arr.
    2. Second is the starting point as alpha1.
    3. The third is the endpoint as alpha2.
  • N space-separated array elements.

Output:

  • Space-separated indexes between alpha1 and alpha2.

Example:

Input:

9 2 6
1 2 3 4 5 6 7 8 9

Output:

1 2 3 4

Solution:

#include <stdio.h>
int main()
{
int starting_point, ending_point, arr[20], length, j;
scanf("%d %d %d",&length,&starting_point,&ending_point);
for(j=0; j<length; j++)
{
scanf("%d",&arr[j]);
}
for(j=0;j<length;j++)
{
if(arr[j]>=starting_point && arr[j]<ending_point)
{
printf("%d ",j);
}
}
return 0;
}

Output

9
2
6
1
2
3
4
5
6
7
8
9
1 2 3 4
n, alpha1, alpha2 = map(int, input().split())
arr = list(map(int, input().split()))
for i in range(n):
    if (arr[i]>=alpha1) and (arr[i]<alpha2):
        print(i, end=" ")
Output:
9 2 6
1 2 3 4 5 6 7 8 9
1 2 3 4 

Question-2

Find the security key to access the bank account in from the encrypted message. The key in the message is the first repeating number from the given message of numbers.

Input:

  • String

Output:

  • The first repeating number from the message

Example:

Input:

123456654321

Output:

1

Solution:

#include <stdio.h> 
#include <string.h>
int main()
{
char arr[20];
int x, y, len, flag=0;
scanf("%s",arr);
len = strlen(arr);
for(x=0;x<len;x++)
{
for(y=x+1;y<len;y++)
{
if(arr[x]==arr[y] && flag == 0)
{
printf("%c",arr[y]);
flag = 1;
break;
}
}
}
return 0;
}

Output

123456654321
1
arr = input()
flag = 0
for i in range(len(arr)):
    for j in range(i+1,len(arr)):
        if (arr[i] == arr[j]) and (flag==0):
            print(arr[j])
            flag = 1
            break;

Output:

123456654321
1

Question-3

Write a program to calculate the sum of elements of an array that are equal to the integer entered by the user. The inputs will be the size of the array, elements of the array, and an integer. You have to print the sum calculated after adding all the elements that match the integer entered by the user.

Input:

  1. Length
  2. Input number
  3. Element

Output:

  • Sum of elements the same as the value

Example:

Input:

8

1 2 3 4 2 5 6 2

2

Output:

6

Solution:

#include<stdio.h>
int sumOfValue(int len, int* arr, int value)
{
int sum = 0;
for(int i =0 ; i < len ; i++)
{
if(arr[i]==value) sum += value;
}
return sum;
}

int main()
{
int len, value, i;
int arr[100];
scanf("%d",&len);
for (i=0;i<len;i++)
{
scanf("%d",&arr[i]);
}
scanf("%d",&value);
printf("%d",sumOfValue(len,arr,value));
}

Output

8
1
2
3
4
5
2
6
2
2
6
size = int(input())
arr = list(map(int,input().split()))
num = int(input())
res = 0
for i in arr:
if i==num:
res+=i
print(res)

Output:

8
1 2 3 4 5 2 6 2
2
6

Question-4

For the generation of energy at the atomic center the energy is used to start the plant is in negative sign and the energy generated after the plant started is represented with a positive sign. Our task is to calculate the total number of energy that is generated throughout the day.

Input:

  • Two inputs –  One for the number of times energy was recorded  as ‘total_reading’ and Second for reading of the energy used or produced ‘arr’.

Output:

  • Sum of the total energy produced

Example:

Input:


1 4 -5 6 7 -4 4 -1

Output:

12

Solution:

total_reading = int(input())
arr = list(map(int,input().split()))
print(sum(arr))

Output:

8
1 4 -5 6 7 4 -4 -1
12
#include<stdio.h>

int main()
{
int total_reading, i, arr[20];
scanf("%d",&total_reading);
for(i=0; i<total_reading; i++)
{
scanf("%d",&arr[i]);
}
int sum = 0;
for(i = 0;i<total_reading;i++)
{
sum+=arr[i];
}
printf("%d",sum);
return 0;
}

Output

8
1  
4
-5
6
7
4
-4
-1
12

Question-5

Print the number at the given index of the series 1 1 2 3 5… and so on. Consider the index as the number and print the value

Input:

  • Single input of the index of the number as ‘number’

Output:

  • The number at the given index of the series

Example:

Input:

10

Output:

55

Solution:

#include <stdio.h>
int main()
{
int val1 = 0, val2 = 1, val3 = 1;
int number, i;
scanf("%d",&number);
for(i=2; i<=number; i++)
{
val3 = val1 +val2;
val1 = val2;
val2 = val3;

}
printf("%d ",val3);
return 0;
}

Output

10
55
index = int(input())
val1, val2, val3 = 0, 1, 1
for i in range(3, index+1):
val1 = val2
val2 = val3
val3 = val1 + val2
print(val3)

Output:

10
55

Question-6

The function printPattern(int  num)​ prints elements of an pre inputted array based on the value of input argument num​  ​(num>=​ 0). If the input number num​ is even, the function is expected to print all the even numbers upto num of the array, and in case it’s odd, is expected to print all the odd numbers upto num of the array.

Input:

  • Num , where 0 <= num <= 10

Output:

  • All numbers separated with space.

Example:

Input:

Num = 2

Arr[] = {1,2,3,4,5,6,7,8,9,10}

Output:

 the function prints 2(without quotes).

Solution:

#include <stdio.h>
void printPattern(int num)
{
int i ;
int arr[] = {1,2,3,4,5,6,7,8,9,0};


if(num%2==0)
{

for(i=0;i<=num;i++)
{
if(arr[i]%2==0)
printf(" %d",arr[i]);
}
}
else
{
for(i=0;i<=num;i++)
{
if(arr[i]%2 != 0)
printf(" %d",arr[i]);
}
}
}

int main()
{
int num=9;
printPattern(num);
return 0;
}

Output


1 3 5 7 9
def printPattern(num):
arr = [1,2,3,4,5,6,7,8,9,0]
if num%2 == 0:
for i in range(num+1):
if arr[i]%2==0:
print(arr[i],end=" ")
else:
for i in range(num+1):
if arr[i]%2!=0:
print(arr[i],end=" ")

num = int(input())
printPattern(num)

Output


6
2 4 6