OA 4 questions

208
Mar 18, 2026
Mar 18, 2026

Q1
Implement a function

int count(vector<char> input)

You are given a sequence of characters consisting of lowercase and uppercase English letters (‘a’–‘z’, ‘A’–‘Z’). Count how many times the user changes the keyboard input in terms of letter difference, ignoring case.
Rules:
Changing between the same letter with different cases (e.g., ‘a’ to ‘A’) does not count as a change

Changing between different letters (e.g., ‘C’ to ‘b’) counts as one change

Example:
input = [‘a’, ‘A’, ‘b’]
Output = 1

Q2
Implement a function

int scooterDist(int finish, vector<int>& scooters)

A person starts at position 0 and wants to reach position finish. Along the way, there are scooters at certain positions.
Rules:
The person moves forward only and cannot go backward

If the person reaches a position with a scooter while walking, they must take it immediately

Once riding a scooter starting at position x, they can ride up to position x + 10

While riding a scooter, any scooters passed along the way are ignored and cannot be used later

After finishing a ride, the person continues walking until they encounter the next scooter (if any)

Return the total distance traveled while riding scooters.
Example:
finish = 23
scooters = [8, 4, 16]
Process:
Start at 0, walk to 4 → must take scooter at 4, ride to 14

While riding from 4 to 14, pass scooter at 8 → cannot use it

At 14, walk to 16 → take scooter at 16, ride to 23

Total scooter distance = (14 - 4) + (23 - 16) = 10 + 7 = 17
Return 17

Q3
Implement a function

vector<string> convert(vector<vector<string>>& paragraph, int width)

You are given a paragraph represented as a list of lines, where each line is a list of words. You need to format the text inside a box of fixed width.
Rules:
The top and bottom of the box are filled with ‘*’ of length width

Each line is wrapped with ‘*’ on both sides

Words in the same line are joined by a single space

The text is centered within the width

Padding spaces are added to both sides so that the total length is width

If the padding is not even, the right side should have more spaces than the left
Example 1
width = 18
Input:
{ "I", "love", "coding" }
{ "ChatGPT" }
Output:

******************
* I love coding  *
*    ChatGPT     *
******************

Example 2
width = 20
Input:
{ "OpenAI", "is", "cool" }
{ "AI" }
Output:

********************
* OpenAI is cool   *
*        AI        *
********************

Explanation for second line:
"AI" length = 2
Total inner space = 20 - 2 - 2 = 16
Left = 8, Right = 8

Q4
Implement a function
string result(vector state, vector& ops).
You are given an initial binary array state consisting of 0s and 1s, and a list of operations.
Operations:
"L": find the leftmost index where the value is 0 and set it to 1

"C{id}": set the value at index id to 0

Process all operations in order and return the final state as a string.
Example:
state = [0, 0, 1, 1, 0, 1]
ops = ["L", "L", "C3", "C0", "L"]
Output:
"111001"

Note: linear solution in "L" operation will time limit exceeded

Comments (1)