0% found this document useful (0 votes)
13 views5 pages

DS Lab Questions

The document contains summaries of problems related to arrays, strings, linked lists, queues, and binary trees. Specifically: 1) The first problem asks to determine if you can reach the last index of a non-negative integer array by jumping distances indicated by each array element. 2) The second problem asks to remove duplicate letters from a string such that each letter appears once using a stack, with the output in lexicographical order. 3) The third problem asks to split a linked list into k consecutive parts with sizes differing by at most one. 4) The fourth problem asks to implement a queue that supports push and pop operations in the front, middle, and back. 5) The

Uploaded by

Vidhya.M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

DS Lab Questions

The document contains summaries of problems related to arrays, strings, linked lists, queues, and binary trees. Specifically: 1) The first problem asks to determine if you can reach the last index of a non-negative integer array by jumping distances indicated by each array element. 2) The second problem asks to remove duplicate letters from a string such that each letter appears once using a stack, with the output in lexicographical order. 3) The third problem asks to split a linked list into k consecutive parts with sizes differing by at most one. 4) The fourth problem asks to implement a queue that supports push and pop operations in the front, middle, and back. 5) The

Uploaded by

Vidhya.M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. You are originally located at the first index of an array of non-negative numbers.

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.

Return an array of the k parts.

Example 1:

Input: head = [1,2,3], k = 5

Output: [[1],[2],[3],[],[]]
Explanation:

The first element output[0] has output[0].val = 1, output[0].next = null.

The last element output[4] is null, but its string representation as a ListNode is [ ].

Example 2:

Input: head = [1,2,3,4,5,6,7,8,9,10], k = 3

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.

Implement the FrontMiddleBack class:

 FrontMiddleBack() Initializes the queue.


 void pushFront(int val) Adds val to the front of the queue.
 void pushMiddle(int val) Adds val to the middle of the queue.
 void pushBack(int val) Adds val to the back of the queue.
 int popFront() Removes the front element of the queue and returns it. If the
queue is empty, return -1.
 int popMiddle() Removes the middle element of the queue and returns it. If
the queue is empty, return -1.
 int popBack() Removes the back element of the queue and returns it. If the
queue is empty, return -1.

Notice that when there are two middle position choices, the operation is performed on
the frontmost middle position choice. For example:

 Pushing 6 into the middle of [1, 2, 3, 4, 5] results in [1, 2, 6, 3, 4,


5].
 Popping the middle from [1, 2, 3, 4, 5, 6] returns 3 and results in [1, 2,
4, 5, 6].

Example 1:

Input:
["FrontMiddleBackQueue", "pushFront", "pushBack", "pushMiddle", "pushMiddle",
"popFront", "popMiddle", "popMiddle", "popBack", "popFront"]

[[], [1], [2], [3], [4], [], [], [], [], []]

Output:

[null, null, null, null, null, 1, 3, 4, 2, -1]

Explanation:

FrontMiddleBackQueue q = new FrontMiddleBackQueue();

q.pushFront(1); // [1]

q.pushBack(2); // [1, 2]

q.pushMiddle(3); // [1, 3, 2]

q.pushMiddle(4); // [1, 4, 3, 2]

q.popFront(); // return 1 -> [4, 3, 2]

q.popMiddle(); // return 3 -> [4, 2]

q.popMiddle(); // return 4 -> [2]

q.popBack(); // return 2 -> []

q.popFront(); // return -1 -> [] (The queue is empty)

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.

Input: deck = [17,13,11,2,3,5,7]

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 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].

We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].

We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].

We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].

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:

Input: deck = [1,1000]

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.

Input: root = [3,2,3, null,3, null,1]

Output: 7
Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

Example 2:

Input: root = [3,4,5,1,3, null,1]

Output: 9

Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy