Flipkart Interview Question
SDE-2sCountry: United States
Interview Type: In-Person
package machinecoding;
public class FinateStateMachine {
// 2. states
private State[] states = { new A(), new B(), new C() };
// 4. transitions
private int[][] transition = { { 2, 1, 0 }, { 0, 2, 1 }, { 1, 2, 2 } };
// 3. current
private int current = 0;
private void next(int msg) {
current = transition[current][msg];
}
// 5. All client requests are simply delegated to the current state object
public void on() {
states[current].on();
next(0);
}
public void off() {
states[current].off();
next(1);
}
public void ack() {
states[current].ack();
next(2);
}
public static void main(String[] args) {
FinateStateMachine fsm = new FinateStateMachine();
int[] msgs = {2, 1, 2, 1, 0, 2, 0, 0};
for (int msg : msgs) {
if (msg == 0) {
fsm.on();
} else if (msg == 1) {
fsm.off();
} else if (msg == 2) {
fsm.ack();
}
}
}
abstract class State {
public void on() {
System.out.println("error");
}
public void off() {
System.out.println("error");
}
public void ack() {
System.out.println("error");
}
}
class A extends State {
public void on() {
System.out.println("A + on = C");
}
public void off() {
System.out.println("A + off = B");
}
public void ack() {
System.out.println("A + ack = A");
}
}
class B extends State {
public void on() {
System.out.println("B + on = A");
}
public void off() {
System.out.println("B + off = C");
}
}
class C extends State {
// 8. The State derived classes only override the messages they need to
public void on() {
System.out.println("C + on = B");
}
}
}
package machinecoding;
public class FinateStateMachine {
// 2. states
private State[] states = { new A(), new B(), new C() };
// 4. transitions
private int[][] transition = { { 2, 1, 0 }, { 0, 2, 1 }, { 1, 2, 2 } };
// 3. current
private int current = 0;
private void next(int msg) {
current = transition[current][msg];
}
// 5. All client requests are simply delegated to the current state object
public void on() {
states[current].on();
next(0);
}
public void off() {
states[current].off();
next(1);
}
public void ack() {
states[current].ack();
next(2);
}
public static void main(String[] args) {
FinateStateMachine fsm = new FinateStateMachine();
int[] msgs = {2, 1, 2, 1, 0, 2, 0, 0};
for (int msg : msgs) {
if (msg == 0) {
fsm.on();
} else if (msg == 1) {
fsm.off();
} else if (msg == 2) {
fsm.ack();
}
}
}
abstract class State {
public void on() {
System.out.println("error");
}
public void off() {
System.out.println("error");
}
public void ack() {
System.out.println("error");
}
}
class A extends State {
public void on() {
System.out.println("A + on = C");
}
public void off() {
System.out.println("A + off = B");
}
public void ack() {
System.out.println("A + ack = A");
}
}
class B extends State {
public void on() {
System.out.println("B + on = A");
}
public void off() {
System.out.println("B + off = C");
}
}
class C extends State {
// 8. The State derived classes only override the messages they need to
public void on() {
System.out.println("C + on = B");
}
}
}
implement using graph and adjacency list
- shubham shandilya November 08, 2016