Skip to content

Commit f3c75af

Browse files
committed
Added common interview programs from hackerrank and codechef
1 parent a88e23b commit f3c75af

File tree

5 files changed

+320
-0
lines changed

5 files changed

+320
-0
lines changed

Arrays/CoinFlip.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Coin Flip
2+
3+
# Little Elephant was fond of inventing new games. After a lot of research, Little Elephant came to know that most of the animals in the forest were showing less interest to play the multi-player games.Little Elephant had started to invent single player games, and succeeded in inventing the new single player game named COIN FLIP.
4+
5+
# In this game the player will use N coins numbered from 1 to N, and all the coins will be facing in "Same direction" (Either Head or Tail),which will be decided by the player before starting of the game.
6+
7+
# The player needs to play N rounds.In the k-th round the player will flip the face of the all coins whose number is less than or equal to k. That is, the face of coin i will be reversed, from Head to Tail, or, from Tail to Head, for i ≤ k.
8+
9+
# Elephant needs to guess the total number of coins showing a particular face after playing N rounds. Elephant really becomes quite fond of this game COIN FLIP, so Elephant plays G times. Please help the Elephant to find out the answer.
10+
11+
# Input
12+
# The first line of input contains an integer T, denoting the number of test cases. Then T test cases follow.
13+
14+
# The first line of each test contains an integer G, denoting the number of games played by Elephant. Each of the following G lines denotes a single game, and contains 3 space separeted integers I, N, Q, where I denotes the initial state of the coins, N denotes the number of coins and rounds, and Q, which is either 1, or 2 as explained below.
15+
16+
# Here I=1 means all coins are showing Head in the start of the game, and I=2 means all coins are showing Tail in the start of the game. Q=1 means Elephant needs to guess the total number of coins showing Head in the end of the game, and Q=2 means Elephant needs to guess the total number of coins showing Tail in the end of the game.
17+
18+
# Output
19+
# For each game, output one integer denoting the total number of coins showing the particular face in the end of the game.
20+
21+
# Constraints
22+
# 1 ≤ T ≤ 10
23+
# 1 ≤ G ≤ 20000
24+
# 1 ≤ N ≤ 109
25+
# 1 ≤ I ≤ 2
26+
# 1 ≤ Q ≤ 2
27+
# Example
28+
29+
# Input:
30+
# 1
31+
# 2
32+
# 1 5 1
33+
# 1 5 2
34+
35+
# Output:
36+
# 2
37+
# 3
38+
# Explanation:
39+
# In the 1st game in Example: I=1, so initial arrangement of coins are H H H H H, and now Elephant will play 5 rounds and coin faces will be changed as follows
40+
# After the 1st Round: T H H H H
41+
# After the 2nd Round: H T H H H
42+
# After the 3rd Round: T H T H H
43+
# After the 4th Round: H T H T H
44+
# After the 5th Round: T H T H T
45+
46+
# Finally Q=1, so we need to find the total number of coins showing Head, which is 2.
47+
48+
# In the 2nd game in Example: This is similar to the 1st game, except Elephant needs to find the total number of coins showing Tail. So the Answer is 3. (Please see the final state of the coins in the 1st game)
49+
50+
# cook your code here
51+
t = input()
52+
t1 = 0
53+
while t1 < t:
54+
g = input()
55+
g1 = 0
56+
while g1 < g:
57+
arr = input().split(' ')
58+
i = int(arr[0])
59+
n = int(arr[1])
60+
q = int(arr[2])
61+
if n%2 == 0:
62+
print(n/2)
63+
else:
64+
if i == q:
65+
print(n/2)
66+
else:
67+
print((n/2)+1)
68+
g1 += 1
69+
t1 += 1
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Trailing Zeros of Factorial
2+
3+
# The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest signal (in a little simplified view). Of course, BTSes need some attention and technicians need to check their function periodically.
4+
5+
# The technicians faced a very interesting problem recently. Given a set of BTSes to visit, they needed to find the shortest path to visit all of the given points and return back to the central company building. Programmers have spent several months studying this problem but with no results. They were unable to find the solution fast enough. After a long time, one of the programmers found this problem in a conference article. Unfortunately, he found that the problem is so called "Traveling Salesman Problem" and it is very hard to solve. If we have N BTSes to be visited, we can visit them in any order, giving us N! possibilities to examine. The function expressing that number is called factorial and can be computed as a product
6+
7+
# 1.2.3.4....N. The number is very high even for a relatively small N.
8+
9+
# The programmers understood they had no chance to solve the problem. But because they have already received the research grant from the government, they needed to continue with their studies and produce at least some results. So they started to study behavior of the factorial function.
10+
11+
# For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbers N1<N2, then Z(N1) <= Z(N2). It is because we can never "lose" any trailing zero by multiplying by any positive number. We can only get new and new zeros. The function Z is very interesting, so we need a computer program that can determine its value efficiently.
12+
13+
# Input
14+
# There is a single positive integer T on the first line of input (equal to about 100000). It stands for the number of numbers to follow. Then there are T lines, each containing exactly one positive integer number N, 1 <= N <= 1000000000.
15+
16+
# Output
17+
# For every number N, output a single line containing the single non-negative integer Z(N).
18+
19+
# Example
20+
# Sample Input:
21+
22+
# 6
23+
# 3
24+
# 60
25+
# 100
26+
# 1024
27+
# 23456
28+
# 8735373
29+
# Sample Output:
30+
31+
# 0
32+
# 14
33+
# 24
34+
# 253
35+
# 5861
36+
# 2183837
37+
38+
t = int(input())
39+
for i in range(t):
40+
num = int(input())
41+
count=0
42+
j=5
43+
while(num//j!=0):
44+
count += int(num/j)
45+
j*=5
46+
print(count)

Queue/DownToZero.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Down to Zero
2+
3+
// You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations on N in each move:
4+
5+
// 1: If we take 2 integers a and b where N = a x b (a!=1, b!=1), then we can change N = max(a,b)
6+
7+
// 2: Decrease the value of N by 1.
8+
9+
// Determine the minimum number of moves required to reduce the value of N to 0.
10+
11+
// Sample Input
12+
// 2
13+
// 3
14+
// 4
15+
16+
// Sample Output
17+
// 3
18+
// 3
19+
20+
// For test case 1, We only have one option that gives the minimum number of moves.
21+
// Follow 3 -> 2 -> 1 -> 0. Hence, 3 moves.
22+
23+
// For the case 2, we can either go 4 -> 3 -> 2 -> 1 -> 0 or 4 -> 2 -> 1 -> 0. The 2nd option is more optimal. Hence, 3 moves.
24+
25+
int main()
26+
{
27+
int max=1000001;
28+
int i, j, nums[max];
29+
30+
//Initializing the array
31+
for(i=0;i<max;i++)
32+
nums[i] = -1;
33+
nums[0] = 0;
34+
nums[1] = 1;
35+
nums[2] = 2;
36+
nums[3] = 3;
37+
38+
for(i=0;i<max;i++)
39+
{
40+
if(nums[i]==-1 || nums[i]>(nums[i-1]+1))
41+
nums[i]=nums[i-1]+1;
42+
for(j=1; j<=i && j*i<max; j++)
43+
if(nums[j*i]==-1 || (nums[i]+1)<nums[j*i])
44+
nums[j*i]=nums[i]+1;
45+
}
46+
47+
int q;
48+
cin>>q;
49+
for(i=0; i<q; i++)
50+
{
51+
int n;
52+
cin>>n;
53+
cout<<nums[n]<< endl;
54+
}
55+
return 0;
56+
}

Strings/Lapindrome.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Lapindromes
2+
3+
# Lapindrome is defined as a string which when split in the middle, gives two halves having the same characters and same frequency of each character. If there are odd number of characters in the string, we ignore the middle character and check for lapindrome. For example gaga is a lapindrome, since the two halves ga and ga have the same characters with same frequency. Also, abccab, rotor and xyzxy are a few examples of lapindromes. Note that abbaab is NOT a lapindrome. The two halves contain the same characters but their frequencies do not match.
4+
# Your task is simple. Given a string, you need to tell if it is a lapindrome.
5+
6+
# Input:
7+
# First line of input contains a single integer T, the number of test cases.
8+
# Each test is a single line containing a string S composed of only lowercase English alphabet.
9+
# Output:
10+
# For each test case, output on a separate line: "YES" if the string is a lapindrome and "NO" if it is not.
11+
# Constraints:
12+
# 1 ≤ T ≤ 100
13+
# 2 ≤ |S| ≤ 1000, where |S| denotes the length of S
14+
# Example:
15+
# Input:
16+
# 6
17+
# gaga
18+
# abcde
19+
# rotor
20+
# xyzxy
21+
# abbaab
22+
# ababc
23+
24+
25+
# Output:
26+
# YES
27+
# NO
28+
# YES
29+
# YES
30+
# NO
31+
# NO
32+
33+
t = int(input())
34+
for i in range(t):
35+
word = input()
36+
word_len = len(word)
37+
mid = word_len//2
38+
if((word_len %2) ==0 ):
39+
str1 = word[0:mid]
40+
str2 = word[mid:]
41+
else:
42+
str1 = word[0:mid]
43+
str2 = word[mid+1:]
44+
45+
str1 = sorted(str1)
46+
str2 = sorted(str2)
47+
if(str1 == str2):
48+
print("YES")
49+
else:
50+
print("NO")

Trees/HuffmanDecoding.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Huffman Decoding
2+
3+
// Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies.
4+
// More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords.
5+
// All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero).
6+
// If on the right, they'll be a 1 (one). Only the leaves will contain a letter and its frequency count.
7+
// All other nodes will contain a null instead of a character, and the count of the frequency of all of it and its descendant characters.
8+
9+
// Input characters are only present in the leaves. Internal nodes have a character value of ϕ (NULL). We can determine that our values for characters are:
10+
11+
// A - 0
12+
// B - 111
13+
// C - 1100
14+
// D - 1101
15+
// R - 10
16+
// Our Huffman encoded string is:
17+
18+
// A B R A C A D A B R A
19+
// 0 111 10 0 1100 0 1101 0 111 10 0
20+
// or
21+
// 01111001100011010111100
22+
23+
// To avoid ambiguity, Huffman encoding is a prefix free encoding technique. No codeword appears as a prefix of any other codeword.
24+
25+
// To decode the encoded string, follow the zeros and ones to a leaf and return the character there.
26+
27+
// You are given pointer to the root of the Huffman tree and a binary coded string to decode. You need to print the decoded string.
28+
29+
// Sample Input
30+
// s="1001011"
31+
32+
// Sample Output
33+
// ABACA
34+
35+
// Explanation
36+
37+
// S="1001011"
38+
// Processing the string from left to right.
39+
// S[0]='1' : we move to the right child of the root. We encounter a leaf node with value 'A'. We add 'A' to the decoded string.
40+
// We move back to the root.
41+
42+
// S[1]='0' : we move to the left child.
43+
// S[2]='0' : we move to the left child. We encounter a leaf node with value 'B'. We add 'B' to the decoded string.
44+
// We move back to the root.
45+
46+
// S[3] = '1' : we move to the right child of the root. We encounter a leaf node with value 'A'. We add 'A' to the decoded string.
47+
// We move back to the root.
48+
49+
// S[4]='0' : we move to the left child.
50+
// S[5]='1' : we move to the right child. We encounter a leaf node with value C'. We add 'C' to the decoded string.
51+
// We move back to the root.
52+
53+
// S[6] = '1' : we move to the right child of the root. We encounter a leaf node with value 'A'. We add 'A' to the decoded string.
54+
// We move back to the root.
55+
56+
// Decoded String = "ABACA"
57+
58+
// The structure of the node is
59+
60+
typedef struct node
61+
{
62+
int freq;
63+
char data;
64+
node * left;
65+
node * right;
66+
67+
}
68+
node;
69+
70+
void decode_huff(node * root, string s)
71+
{
72+
node * treeroot = root;
73+
// char *result = (char*)malloc(sizeof(char)*len);
74+
int i = 0, k = 0;
75+
while (s[i]!='\0')
76+
{
77+
// printf("s[%d]%c\n", i, s[i]);
78+
if (s[i]=='1')
79+
{
80+
root = root -> right;
81+
if (root->data!='\0')
82+
{
83+
printf("%c", root->data);
84+
root = treeroot;
85+
}
86+
i++;
87+
}
88+
else
89+
{
90+
root = root -> left;
91+
if (root->data!='\0')
92+
{
93+
printf("%c", root->data);
94+
root = treeroot;
95+
}
96+
i++;
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)