Flipkart Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

implement using graph and adjacency list

- shubham shandilya November 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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");
}
}

}

- Veeresh Kumar T September 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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");
}
}

}

- Veeresh Kumar T September 07, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More