Memcached Service Codechef Solution

Hello Programmers In this post, you will know how to solve the Memcached Service Codechef Solution.

Memcached Service Codechef Solution
Memcached Service Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Problem

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

Sample Output

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.

Ezoicreport this adMemcached 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
	{
		// your code goes here
			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");
		        }
		}
	}
}
Ezoicreport this ad

Memcached Service CodeChef Solutions 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[10];
		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 Solutions 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 BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Dilemma Codechef Solution

Sharing Is Caring

Leave a Comment