DS Lab Questions
DS Lab Questions
Your maximum leap length at that location is represented by each member in the
array. Check to see if you can get to the last index.
Example 1:
Input: [3,2,2,1,5]
Output: true
Explanation: Jump 1 step from index 0 to index 1, then the index 1 value is 2, so
jump to index3, the index3 value is 1, so jump and reach the last index.
Example 2:
Input: [3,2,1,0,4].
Output: false
Explanation: No matter what, you will always reach index 3. It cannot reach the last
index since its maximum jump length is 0.
2. Give a string, remove duplicate letters from the string s such that each letter appears
just once using Stack. The lexicographical order of your result must be the least of all
feasible outcomes.
Example 1:
Input: s = "bcabc"
Output: "abc"
Example 2:
Input: s = "cbacdcbc"
Output: "acdb"
Linked List:
3. Given the head of a singly linked list and an integer k, split the linked list
into k consecutive linked list parts.
The length of each part should be as equal as possible: no two parts should have a size
differing by more than one. This may lead to some parts being null.
The parts should be in the order of occurrence in the input list, and parts occurring earlier
should always have a size greater than or equal to parts occurring later.
Example 1:
Output: [[1],[2],[3],[],[]]
Explanation:
The last element output[4] is null, but its string representation as a ListNode is [ ].
Example 2:
Output: [[1,2,3,4],[5,6,7],[8,9,10]]
Explanation:
The input has been split into consecutive parts with size difference at most 1, and earlier
parts are a larger size than the later parts.
4. Queue 1:
Design a queue that supports push and pop operations in the front, middle, and back.
Notice that when there are two middle position choices, the operation is performed on
the frontmost middle position choice. For example:
Example 1:
Input:
["FrontMiddleBackQueue", "pushFront", "pushBack", "pushMiddle", "pushMiddle",
"popFront", "popMiddle", "popMiddle", "popBack", "popFront"]
[[], [1], [2], [3], [4], [], [], [], [], []]
Output:
Explanation:
q.pushFront(1); // [1]
q.pushBack(2); // [1, 2]
q.pushMiddle(3); // [1, 3, 2]
q.pushMiddle(4); // [1, 4, 3, 2]
queue2:
You are given an integer array deck. There is a deck of cards where every card has a
unique integer. The integer on the ith card is deck[i].
You can order the deck in any order you want. Initially, all the cards start face down
(unrevealed) in one deck.
You will do the following steps repeatedly until all cards are revealed:
1. Take the top card of the deck, reveal it, and take it out of the deck.
2. If there are still cards in the deck then put the next top card of the deck at the
bottom of the deck.
3. If there are still unrevealed cards, go back to step 1. Otherwise, stop.
Return an ordering of the deck that would reveal the cards in increasing order.
Note that the first entry in the answer is considered to be the top of the deck.
Output: [2,13,3,11,5,17,7]
Explanation:
We get the deck in the order [17,13,11,2,3,5,7] (this order does not matter), and reorder it.
After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
We reveal 11, and move 17 to the bottom. The deck is now [13,17].
We reveal 13, and move 17 to the bottom. The deck is now [17].
We reveal 17.
Since all the cards revealed are in increasing order, the answer is correct.
Example 2:
Output: [1,1000]
Trees:
5. The thief has found himself a new place for his thievery again. There is only one entrance
to this area, called root.
Besides the root, each house has one and only one parent house. After a tour, the smart
thief realized that all houses in this place form a binary tree. It will automatically contact
the police if two directly-linked houses were broken into on the same night.
Given the root of the binary tree, return the maximum amount of money the thief can
rob without alerting the police.
Output: 7
Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
Output: 9