Skip to content

Commit aad295c

Browse files
committed
solution ideas
1 parent f2c3b70 commit aad295c

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
### Document search
22

33
Design an algorithm that takes a sequence of `n` document words and a sequence of `m` query words and find the shortest interval in which the `m` query words appear in the document in the order given. The length of an interval is the number of words in that interval.
4+
5+
### Solution
6+
7+
Create a symbol table of words and their positions in the document. Then, for each query word, find the shortest interval containing all query words.
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
# Design a generalized queue data type that supports all of the following operations in logarithmic time (or better) in the worst case.
1+
# Design an algorithm that takes a sequence of `n` document words and a sequence of `m` query words and find the shortest interval in which the `m` query words appear in the document in the order given. The length of an interval is the number of words in that interval.
22

3-
# - Create an empty data structure.
4-
# - Append an item to the end of the queue.
5-
# - Remove an item from the front of the queue.
6-
# - Return the `i`th item in the queue.
7-
# - Remove the `i`th item from the queue.
8-
9-
# TODO: Implement
3+
def algorithm(document_words, query_words)
4+
next_word = query_words.shift
5+
start_i = nil
6+
document_words.each_with_index do |word, i|
7+
if word == next_word
8+
start_i = i if start_i.nil?
9+
next_word = query_words.shift
10+
return i - start_i if next_word.nil?
11+
end
12+
end
13+
return nil
14+
end

week5/balanced-search-trees/interview-questions/task3/TASK.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ Design a generalized queue data type that supports all of the following operatio
77
- Remove an item from the front of the queue.
88
- Return the `i`th item in the queue.
99
- Remove the `i`th item from the queue.
10+
11+
### Solution
12+
13+
Binary tree sorted by `i`.

week5/balanced-search-trees/interview-questions/task3/solution.rb

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)