---
# System prepended metadata

title: May 22 IDP

---

# May 22 IDP

## Easy - 1

**References:** https://leetcode.com/problems/reverse-string-ii/description/

**Question:** Given a string `S` and an integer `K`, write a program to reverse `K` characters of the given string.


**Variant - 1**: Reverse the first `K` characters of the given string

**Input**:
```
hello
3
```
Output:
```
lehlo
```


**Variant - 2**: Reverse the last `K` characters of the given string

**Input**:
```
hello
3
```
**Output:**
```
heoll
```


## Easy - 2

**References:** https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured/description/

**Question:** An horizontal game board consists of the blocks in a row. The blocks can be a start point, ball, hole. From each start point, check if we can strike atleast a ball into the hole.

Write a program that reads a `game_board`, where start point, ball, hole are represented by "S", "O", "X" respectively.

**Note:**
There will always be some ball/balls between a starting point **S** and a hole **X**.

**Variant - 1**: Print every starting point along with **True** or **False** which denotes that if he can strike balls to a hole on **left** side from that starting point.


**Input**:
```
S O O X O X O O O S O
```
**Output:**
```
S False
S True
```


**Variant - 2**: Print every starting point along with **True** or **False** which denotes that if he can strike balls to a hole on **right** side from that starting point.

**Input**:
```
S O O X O X O O O S O
```
**Output:**
```
S True
S False
```


## Medium - 1

**References:** https://leetcode.com/problems/top-k-frequent-words/

**Question:** Given a sequence of words `companies` representing the names of searched companies throughout the year and a number `N`.

**Variant - 1**: Print the top `N` most searched company names.

**Input**:
```
Apple Matx Sony Formax Sony Formax Matx Apple Sony Softech
3
```
**Output:**
```
Sony
Apple
Forma
```


**Variant - 2**: Print the `N` least searched companies.

**Input**:
```
Apple Matx Sony Formax Sony Formax Matx Apple Sony Softech
3
```
Output:
```
Softech
Matx
Formax
```


## Medium - 2

**References:** https://leetcode.com/problems/similar-string-groups/

**Question:** Given a sequence of anagram words `ancient_words` of different languages. Group the words by language. A word is added to a language group, if it is equal to one of the words in language group, by swapping atmost **1** pair of characters.

**Variant - 1**: Print the number of language groups.

**Input**:
```
mark karm mkar kamr
```
**Output:**

```
2
```


**Variant - 2**: Print the number of languages groups that has only one word.

**Input**:
```
mark karm mkar kamr
```
**Output:**
```
1
```


## Hard

**References:** https://leetcode.com/problems/process-restricted-friend-requests/description/

**Question:** Given the number of users `N`, number of restrictions `M`, `M` pairs of user IDs, that are restricted to not to become friends, number of incoming friend requests `R`, and `R` pairs of user IDs that the friend request is between.

For each incoming friend request, check if it is accepted or rejected. Friend request is accepted, if both can become friends directly or indirectly.

Both can become friends, if the both the ids are not in a pair of restrictions, and all the friends of both ids, are not in a pair of restrictions. If Friend request is accepted, they become friends.

**Variant - 1**: Print each incoming friend request and the status of the friend request (**Accepted** or **Rejected**) separated by a space.

**Input**:
```
5
3
0 1
1 2 
2 3
4
0 4 
1 2 
3 1 
3 4
```
**Output:**

```
0 4 Accepted
1 2 Rejected
3 1 Accepted
3 4 Rejected
```


**Variant - 2**: Print number of **Accepted** and **Rejected** friend requests each on a new line.

**Input**:

```
5
3
0 1
1 2 
2 3
4
0 4 
1 2 
3 1 
3 4
```
**Output:**

```
Accepted 2
Rejected 2
```



