# How To Answer Algorithm Interview Questions? The REACTO Method

It might be tempting to start solving a coding exercise or question as soon as we hear it during technical interviews, especially when we know the answer. Often, interviewers are interested in how we approach problems instead of recalling a memorized solution. Let’s learn how to answer algorithm interview questions using the REACTO method.

**The REACTO method is a six-step technique to answer algorithm questions in technical interviews. REACTO stands for Repeat, Example, Approach, Code, Test, and Optimize. The REACTO method helps candidates understand and clarify questions and show their ability to think through the solution to complex problems.**

# The REACTO Method With an Example

Let’s go through each step of the REACTO method to solve a sample interview question. Let’s find out if an expression has balanced parenthesis. Every opening parenthesis should have a matching closing parenthesis in the appropriate order.

Let’s follow the REACTO method.

## Step 1: R Stands for Repeat

First, before rushing to write a solution, understand the problem by paraphrasing it in your own words. Also, ask clarifying questions about input size, constraints, edge cases, assumptions, and other questions to help you solve the problem.

For example, let’s repeat the question of our balanced parenthesis exercise:

*“Ok…I need to find if an expression has balanced parenthesis, right? Should I also consider square or curly brackets? Does the input string contain other characters, like numbers or symbols? Do I need to validate for null or empty strings as input?”*

By repeating the interview question and asking for clarifications, we understand the problem and give ourselves some time to think.

## Step 2: E Stands for Example

After understanding the problem better, the next step of the REACTO method is to think of a list of examples. Write a list of two or three input values and their expected results. Also, think of some edge cases.

Let’s write a list of examples for our exercise:

*“Now, let me write a list of expressions with balanced and unbalanced parenthesis. For example, these expressions have balanced parenthesis: ‘()’, ‘(())’, ‘()()()’. But, these ones don’t: ‘(()’, ‘)’. Am I right?”*

## Step 3: A Stands for Approach

After understanding the question and writing some examples, the next step of the REACTO method is to state the approach to solve the problem. Say how you will solve the problem (using a recursive or iterative solution, for example) and what data structures you need.

Feel free to start with a “brute force” solution in the first attempt. Tell your interviewer that the brute force solution is your initial step and clarify if you can start with that approach.

Let’s do the Approach step for our exercise:

*“To solve this problem, I will iterate through the input expression and put the opening parenthesis in a stack. Later, if I find a closing parenthesis, I pop one element from the stack to see if it matches. Does it sound right? Can I use this approach?”*

## Step 4: C Stands for Code

The next step after stating the approach is to code a solution. Start with a top-down approach, laying out your steps with function declarations. You can fill in the details later. Don’t work in silence. Guide the interviewer through your code.

For example, let’s do the Code step for our exercise:

*“Let me start by writing a function declaration. I’m calling it: ‘has_balanced_parenthesis’. It receives a string and returns a boolean. Next, I’m declaring a stack and looping through the input string…”*

Using pseudocode, we can write something like this,

def has_balanced_parenthesis(input):

stack = []

for c in input:

if c == '(':

stack.push(c)

else:

if stack is empty or stack.pop() != '(':

return Falsereturn len(stack) == 0

## Step 5: T Stands for Test

The next step is to test your code. Evaluate your solution with one of the examples from the Example step. Go through your code, line by line, as if you were a computer. Go back to your solution if you find flaws in it.

For example, let’s take a simple input to evaluate our solution:

*“Let me validate my solution with a simple input expression. For example: ‘()’…The first character is a ‘(‘. So I push it into the stack. The next character is a ‘)’. The stack isn’t empty, and the popped element is a ‘(‘. That’s the end of the input. And the end, the stack is empty. So this expression has balanced parenthesis…”*

## Step 6: O Stands for Optimize

The last step of the REACTO method is to optimize your solution. If you used a brute force solution during the Code step, the interviewer could start a conversation about how to write a better solution.

During this step, the interviewer could also ask you about your code’s runtime complexity. Think of how your solution will behave, in terms of computing time and space, if the input gets 10x or 100x bigger.

Let’s do the Optimize step for our solution:

*“Well, since this code goes through a given expression in a loop, the number of operations grows if the input size grows. So I would say it has linear complexity. Also, in a very edge case, if I only get opening parenthesis, the stack size will grow as the input grows. So, it also has linear complexity in terms of space.”*

# Next steps

To master the REACTO method, start practicing with problems you know how to solve, focusing on every step of the method instead of the solution.

In your study sessions, recreate the scenario of your interviews. Practice solving a problem in a text editor or whiteboard instead of an IDE with auto-completion. Go through every step of the REACTO method, thinking out loud, as you would do in a technical interview. And to make your study sessions more realistic, time yourself. Try to solve an exercise in 30 or 40 minutes.

Start your practice sessions with a similar exercise to the one we already went through: find the length of the longest valid or well-formed parenthesis substring. For example, ‘(()()’ has two valid parentheses and, therefore, its length is 4.

If you need more exercises to practice, try any of these: find two elements that add to a given number, evaluate a postfix expression, rotate the elements of an array, and remove the nth node from the end of a linked list.

In your future technical interviews, don’t rush to code. Use the REACTO method instead.

*Stay tuned to **Klever for Solutions** for more content about careers, interviews, and programming in general. Follow Klever for Solutions on **LinkedIn** too.*