Skip to content

Commit cb516de

Browse files
Create BranTeaser_questions.md
1 parent 371365e commit cb516de

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## List of Common Brain Teaser Questions
2+
3+
1. Array Game
4+
2. Prefix Hierarchy
5+
3. The Largest String
6+
7+
8+
## Questions and Answer
9+
10+
### **Array Game**
11+
***Questions:***
12+
```
13+
Given an array of integers, determine the number of moves to make all elements equal. Each move consists of choosing all but 1 element and incrementing their values by 1.
14+
15+
16+
Example
17+
18+
numbers = [3, 4, 6, 6, 3]
19+
```
20+
21+
***Solutions:***
22+
```
23+
Concepts covered: Ad-hoc algorithm, array traversal
24+
25+
Optimal Solution:
26+
27+
Because our goal is to make all elements the same, instead of choosing all but 1 element and incrementing their values by 1, we can choose 1 element and decrease its value by 1 while keeping the rest of the elements the same. Under such conditions (when only the chosen element gets decreased) the only element that shouldn't be decreased is the minimum element of the array. All other elements have to be decreased until they all will be equal to the minimal element. Therefore the answer can be calculated as the sum of differences between each element of the array and the minimum element of the array.
28+
```
29+
30+
### **Prefix Hierarchy**
31+
32+
***Questions:***
33+
```
34+
Given a list of names, determine the number of names in that list for which a given query string is a prefix. The prefix must be at least 1 character less than the entire name string.
35+
36+
Example
37+
38+
names = ['jackson', 'jacques', 'jack']
39+
40+
query = ['jack']
41+
42+
The complete query string 'jack' is a prefix of jackson but not of jacques or jack. The prefix cannot contain the entire name string, so 'jack' does not qualify.
43+
44+
Function Description
45+
46+
Complete the function findCompletePrefixes in the editor below. The function must return an array of integers that each denotes the number of names strings for which a query string is a prefix.
47+
48+
49+
findCompletePrefixes has the following parameter(s):
50+
51+
string names[n]: an array of name strings
52+
53+
string query[q]: an array of query strings
54+
```
55+
56+
***Solutions:***
57+
```
58+
Concepts covered: Ad-hoc algorithm, array traversal
59+
60+
Optimal Solution:
61+
62+
Because our goal is to make all elements the same, instead of choosing all but 1 element and incrementing their values by 1, we can choose 1 element and decrease its value by 1 while keeping the rest of the elements the same. Under such conditions (when only the chosen element gets decreased) the only element that shouldn't be decreased is the minimum element of the array. All other elements have to be decreased until they all will be equal to the minimal element. Therefore the answer can be calculated as the sum of differences between each element of the array and the minimum element of the array.
63+
```

0 commit comments

Comments
 (0)