Programming 1
Qn - 1
You are an adventurer. You have found N dungeons. Some are open, and others are closed. All dungeons contain a valuable item that will be useful in the future. You should always start from the left and go to the right. Once you retrieve the item from a dungeon and come out, the dungeons that were closed will be opened, and the ones that were open will be closed. So your task is to find the number of valuable items that you can retrieve from these dungeons.
To represent the state of the dungeon,Â
0 - Closed dungeon
1 - Opened dungeon
Input
N - number of dungeons
l - list of dungeons and their states
Output
Return the number of valuable items retrieved from the dungeons
Sample Input 1:
7
[1,1,0,0,0,1,0]
Sample Output 1:
4
Sample Input 2:
3
[1,1,1]
Sample Output 2:
1
Sample Input 3:
5
[1,0,1,1,1]
Sample Output 3:
3
Note: Solve this question using Python.
Qn - 2
Input:Â
n number of phone numbersÂ
p phone numbers
Objective:
Given n number of phone numbers from a city your goal is to find the length of the city phone code within the given phone numbers. The city phone code is present at the prefix (the beginning) of each phone number, it can go up to length p//2. If the length of phone numbers doesn't match or if there is no similar pattern in the given set of phone numbers print -1.
The number of test cases will be the first line of the input, followed by n for each test case.
Sample input:
3
3
00209
00219
00999
4
7334234546253434
7334234862544242
7334234336415424
7334234363654132
3
637676232
637223
63721244241
Output:
2
7
-1
Note: Solve this question using Python.
Qn - 3
Eminem and Rihanna are in a binary forest (a forest filled with 1s and 0s only).
If a tree is 0, you can see through it. You cannot see through trees that are 1. Eminem and Rihanna both start arguing over whose eyesight is better, and who can see farther. Rihanna claims she can see further, whereas Eminem knows for a fact that he can actually see farther.Â
They come to a truce - they will play a game. Starting from outside the forest initially (even if the first tree is 1), both of them will stand at each "1" tree, and note down the farthest point to which they can see (they stop looking when they see the next "1" tree). At the end of all of them, both of them will state the farthest point to which they can see, and both of them have agreed to be honest. One of them is definitely correct.Â
Your task is to verify which one of them is correct. Find out the actual farthest distance and verify which of the artists is correct!
Input format:Â
t -> number of test cases
n -> number of trees in the forestÂ
n integers -> the value of each treeÂ
Example:
Input:
5
5
1 0 0 1 0
4
0 1 1 1
1
0
3
1 1 1
9
1 0 0 0 1 0 0 0 1
Output:
2
1
1
0
3
Note: Solve this question using Python.
Qn - 4
We have invented our own sequence of numbers. The sequence is generated by calculating the XOR value of the previous 2 numbers in the sequence.Â
Input:
t
x y
n
Where x and y are the first 2 numbers in the sequence, and you need to return the nth number from the sequence (index-wise).Â
Sample input:
2
3 8Â
4
43 2
10943
Sample output:
8
41
Note: Solve this question using C.
Qn - 5
You and your friend are playing a game with magic leaves. There are 2 types of leaves in the big pileÂ
(think of it as a deck/stack of leaves, where only the top leaf is accessible at any given time).Â
The 2 types of leaves are:Â
    - Golden leaf -> the magic of this leaf is 0Â
    - Silver leaf -> the magic of this leaf is always > 0
You have a separate bucket where you collect leaves from this pile.Â
You can do the following with this pile of leaves:Â
    - take a leaf from the top of the pileÂ
    - if this leaf is golden, you can either choose to start over, or keep your current magic as final and end the game
    - if this leaf is silver, then the magic of the top leaf in your bucket is added to its magic (if it is not empty),
        after that the leaf is added to your bucket, and the used silver leaf is discarded
Your objective is to find the right set of moves (when to/not to start over), and hence find the maximum possible magic from the given set of leaves.
Input format:
t -> number of test casesÂ
n -> number of leaves in the pileÂ
n integers, containing the magic of each leaf in top-down orderÂ
Example:
Input:
6
5
3 3 3 0 0
6
0 3 3 0 0 3
7
1 2 3 0 4 5 0
7
1 2 5 0 4 3 0
5
3 1 0 0 4
5
1 3 0 4 2
Output:
9
6
9
8
4
6
Note: Solve this question using C/C++.