0%
0 / 9 answered
Algorithms with Selection and Repetition Practice Test
•9 QuestionsQuestion
1 / 9
Q1
A traffic light cycles through GREEN, YELLOW, RED for a fixed number of cycles. Inputs are cycles and starting state; output is the printed sequence of states. Example input: cycles 1, start GREEN; output: GREEN, YELLOW, RED. Example input: cycles 2, start RED; output: RED, GREEN, YELLOW, RED, GREEN, YELLOW.
String state = startState;
for (int c = 0; c < cycles; c++) {
for (int t = 0; t < 3; t++) {
System.out.println(state);
if (state.equals("GREEN")) state = "YELLOW";
else if (state.equals("YELLOW")) state = "RED";
else state = "GREEN";
}
}
Consider the algorithm for the traffic light; what is the time complexity in terms of cycles $c$?
A traffic light cycles through GREEN, YELLOW, RED for a fixed number of cycles. Inputs are cycles and starting state; output is the printed sequence of states. Example input: cycles 1, start GREEN; output: GREEN, YELLOW, RED. Example input: cycles 2, start RED; output: RED, GREEN, YELLOW, RED, GREEN, YELLOW.
String state = startState;
for (int c = 0; c < cycles; c++) {
for (int t = 0; t < 3; t++) {
System.out.println(state);
if (state.equals("GREEN")) state = "YELLOW";
else if (state.equals("YELLOW")) state = "RED";
else state = "GREEN";
}
}
Consider the algorithm for the traffic light; what is the time complexity in terms of cycles $c$?