forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ICanGuessTheDataStructure.java
173 lines (126 loc) · 4.8 KB
/
ICanGuessTheDataStructure.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// There is a bag-like data structure, supporting two operations:
// 1 x Throw an element x into the bag.
// 2 Take out an element from the bag.
// Given a sequence of operations with return values, you’re going to guess the data structure. It is
// a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger
// elements first) or something else that you can hardly imagine!
// Input:
// There are several test cases. Each test case begins with a line containing a single integer n (1 ≤ n ≤
// 1000). Each of the next n lines is either a type-1 command, or an integer 2 followed by an integer x.
// That means after executing a type-2 command, we get an element x without error. The value of x
// is always a positive integer not larger than 100. The input is terminated by end-of-file (EOF).
// Output:
// For each test case, output one of the following:
// stack It’s definitely a stack.
// queue It’s definitely a queue.
// priority queue It’s definitely a priority queue.
// impossible It can’t be a stack, a queue or a priority queue.
// not sure It can be more than one of the three data structures mentioned
// above.
// Sample Input
// 6
// 1 1
// 1 2
// 1 3
// 2 1
// 2 2
// 2 3
// 6
// 1 1
// 1 2
// 1 3
// 2 3
// 2 2
// 2 1
// 2
// 1 1
// 2 2
// 4
// 1 2
// 1 1
// 2 1
// 2 2
// 7
// 1 2
// 1 5
// 1 1
// 1 3
// 2 5
// 1 4
// 2 4
// Sample Output
// queue
// not sure
// impossible
// stack
// priority queue
/**
* Created by kdn251 on 2/10/17.
*/
import java.util.*;
import java.io.*;
public class ICanGuessTheDataStructure {
public static void main(String args[]) throws Exception {
//initialize data structures
Stack<Integer> stack = new Stack<Integer>();
Queue<Integer> queue = new LinkedList<Integer>();
//initialize max priority queue
PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>(Collections.reverseOrder());
//initialize buffered reader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
//iterate through all test cases
while ((line = br.readLine()) != null) {
//initialize removals for each data structure
int stackRemovals = 0;
int queueRemovals = 0;
int priorityQueueRemovals = 0;
int totalRemovals = 0;
//get number of test cases
int numberOfCases = Integer.parseInt(line);
//clear contents of data structures
queue.clear();
priorityQueue.clear();
stack.clear();
//iterate over all test cases
for (int i = 0; i < numberOfCases; i++) {
String[] currentLineSplit = br.readLine().split(" ");
int command = Integer.parseInt(currentLineSplit[0]);
int number = Integer.parseInt(currentLineSplit[1]);
//if command is 1, push number into all data structures
if (command == 1) {
stack.push(number);
queue.add(number);
priorityQueue.add(number);
} else {
//check which data structure to remove from and increment its removal count
if (!stack.isEmpty() && stack.peek() == number && stackRemovals == totalRemovals) {
stackRemovals++;
stack.pop();
}
if (!queue.isEmpty() && queue.peek() == number && queueRemovals == totalRemovals) {
queueRemovals++;
queue.remove();
}
if (!priorityQueue.isEmpty() && priorityQueue.peek() == number && priorityQueueRemovals == totalRemovals) {
priorityQueueRemovals++;
priorityQueue.remove();
}
totalRemovals++;
}
}
//check all removal counts for each data structure vs. total removal count and print the appropriate data structure
if ((stackRemovals == totalRemovals && queueRemovals == totalRemovals) || (stackRemovals == totalRemovals && stackRemovals == priorityQueueRemovals) || (queueRemovals == totalRemovals && priorityQueueRemovals == totalRemovals)) {
System.out.println("not sure");
} else if (stackRemovals == totalRemovals) {
System.out.println("stack");
} else if (queueRemovals == totalRemovals) {
System.out.println("queue");
} else if (priorityQueueRemovals == totalRemovals) {
System.out.println("priority queue");
} else {
System.out.println("impossible");
}
}
}
}