# Memcached Service Codechef Solution

Memcached Service Codechef Solution: Chef is a programmer. In his computer, he is running a caching service, Memcached. He can give the commands “start”, “restart” or “stop” to the service, the functionalities of which are specified below.

• “start”: Start the service.
• “restart”: If the service is started, do nothing. Otherwise, start the service.
• “stop”: If the service is not running, give an error. Otherwise, stop the service.

The service is initially not running. You are given nn commands that he then gives to the program in sequence. Your task is to identify whether some error/s were encountered while running these commands.

### Input

• The first line of each test case contains an integer TT denoting the number of test cases. The description of TT test cases follows.
• The first line of each test case contains an integer nn, denoting the number of commands.
• The next line contains nn space separated strings, each of which is either “start”, “restart” or “stop”.

### Output

For each test case, output “404” if some error occurred, otherwise output “200” (without quotes).

### Constraints

• 1≤T≤1001≤T≤100
• 1≤n≤101≤n≤10

### Sample Input

```5
2
start stop
2
restart stop
3
start restart stop
3
start stop stop
1
stop
```

```200
200
200
404
404
```

### Explanation

• In the first, second and third cases, there won’t be any error.
• In the fourth case, when you execute the third command, the service is already stopped. So, it will result in an error.
• In the fifth case, you are trying to stop the service when it’s not even started, and will hence result in an error.

## Memcached Service – CodeChef Solution in JAVA

```import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
{
int n=s.nextInt();
String[] a=new String[n];
for(int i=0;i<n;i++)
{
a[i]=s.next();
}
int flag=0,temp=0;
for(int j=0;j<n;j++)
{
if(a[j].equals("stop") && flag==0)
{
temp=1;
break;
}
else if(a[j].equals("stop"))
{
flag=0;
}
else if(a[j].equals("start") || a[j].equals("restart"))
{
flag=1;
}
}
if(temp==1)
{
System.out.println("404");
}
else
{
System.out.println("200");
}
}
}
}
```

## Memcached Service – CodeChef Solution in CPP

```#include<bits/stdc++.h>
#include <string.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t,i,error;
cin>>t;
while(t--)
{
int n;
cin>>n;
i=0;
error=0;
while(n--)
{
char s;
cin>>s;
if(strcmp(s,"stop")==0)
{
i--;
if(i<0)
{
error=1;
}
}
else if(strcmp(s,"start")==0)
{
i=1;
}
else if(strcmp(s,"restart")==0)
{
i=1;
}
}
if(error==0)
{cout<<"200"<<endl;
}
else if(error==1)
{cout<<"404"<<endl;
}
}
}```

## Memcached Service -CodeChef Solution in Python

```t = int(input())
for i in range(t):
n = int(input())
c = 1
l = list(input().split())
for j in range(n):
if j == 0:
if l[j] == 'stop':
c -= 1
break
elif j < n - 1:
if l[j] == 'stop' and l[j + 1] == 'stop':
c -= 1
break
print('200' if c == 1 else '404')```

Disclaimer: The above Problem (Memcached Service ) is generated by CodeChef but the solution is provided by  Chase2learn.This tutorial is only for Educational and Learning purpose.

Sharing Is Caring