Thread: interview Q and A i encountered - BOKIBOAZ

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    interview Q and A i encountered - BOKIBOAZ

    hi dudes
    had a job interview to implement a transformation protocol between A and B with 5 different massages types, the solution was :

    Code:
    #include "stateMachine.h"
    //#include "stdafx.h"
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    #define VALID 1
    #define INVALID 0
    
    #define D_A 0
    #define D_B 1
    
    #define ARR_SIZE 25
    
    typedef struct{
    	char cmdArr[ARR_SIZE];
    	bool AorB;
    }cmd_s;
    
    typedef struct{
    	char cmdArr[ARR_SIZE];
    	int size;
    }cmd3_s;
    
    
    //Global Variables
    cmd_s Cmd;
    cmd3_s Cmd3;
    
    
    void fillArr(char destArr[], const char sourceArr[], const int size){
    	memset(destArr, 0, ARR_SIZE);
    	memcpy(destArr, sourceArr, size);
    }
    
    bool isCmd(const char * cmd_t, const int size){
    	if(strncmp(Cmd.cmdArr, cmd_t, size) == 0){
    		return 1;
    	}
    	return 0;
    }
    
    int checkCmd(bool drction, char * cmd_t, int size, int *statePtr, int dest_state){
    	if(Cmd.AorB == drction ){
    		if(isCmd(cmd_t, size)){
    			*statePtr = dest_state;
    			return VALID;
    		}
    	}
    
    	*statePtr = 1;
    	return INVALID;
    }
    
    void saveCmd3(const char sourceArr[], const int size){
    	fillArr(Cmd3.cmdArr, sourceArr, size);
    	Cmd3.size = size - 1; //don't count the '/0'
    }
    
    int checkState3(int *statePtr){
    	int status = (checkCmd(D_A,"LIST", sizeof("LIST"),statePtr, 4));
    
    	saveCmd3("LIST", sizeof("LIST"));
    
    	// other possible commands ...	
    
    	if(status == INVALID){
    		status = (checkCmd(D_A,"EXIT ", sizeof("EXIT "),statePtr, 5));
    	}
    
    return status;
    
    }
    
    int getCmdLen(){
    	int i=0;
    	int cnt=0;
    
    	for(i=0; i<ARR_SIZE; i++){
    		if(Cmd.cmdArr[i] != '\0')
    			cnt++;
    	}
    
    return cnt;
    }
    
    int checkCh(char ch){
    	if((ch >='a') && (ch <='z'))
    		return VALID;
    
    	if((ch >='0') && (ch <='9'))
    		return VALID;
    
    return INVALID;
    
    }
    
    int checkState4(int *statePtr){
    	int cmdSize = getCmdLen();
    	int cmd3Size = Cmd3.size;
    	int SecondIdx = cmdSize - cmd3Size;
    	int i;
    
    	*statePtr = 1;
    
    	if(cmdSize < cmd3Size*2 + 2)// check if the cmd msg is twice+2 space the cmd3 msg
    		return INVALID;
    
    	if(strncmp(&Cmd.cmdArr[0], Cmd3.cmdArr, cmd3Size) != 0)
    		return INVALID;
    
    	if(strncmp(&Cmd.cmdArr[SecondIdx], Cmd3.cmdArr, cmd3Size) != 0)
    		return INVALID;
    
    	// check the msg
    	for(i = cmd3Size+1; i<= SecondIdx-2 ; i++ ){
    		if(checkCh(Cmd.cmdArr[i]) == INVALID)
    			return INVALID;
    	}
    
    	*statePtr = 3;
    	return VALID;
    
    }
    
    int handleCmd(){
    	static int stat_state = 1;
    
    	switch(stat_state){
    		case 1:
    			return (checkCmd(D_A,"OPEN_CONNECTION", sizeof("OPEN_CONNECTION"),&stat_state, 2));
    		case 2:
    			return (checkCmd(D_B,"OPEN_CONNECTION_ACKED", sizeof("OPEN_CONNECTION_ACKED"),&stat_state, 3));
    		case 3:
    			return (checkState3(&stat_state));
    		case 4:
    			return (checkState4(&stat_state));
    		case 5:
    			return (checkCmd(D_B,"EXIT_ACKED", sizeof("EXIT_ACKED"),&stat_state, 1));
    
    		default: 
    			return INVALID;
    
    	}
    }
    
    int stateMachine::main2(){
    	// init
    
    	memset(&Cmd, 0, sizeof(Cmd) );
    	memset(&Cmd3, 0, sizeof(Cmd3));
    
    	Cmd.AorB = D_A;
    	fillArr(Cmd.cmdArr, "OPEN_CONNECTION", sizeof("OPEN_CONNECTION"));
    	handleCmd();
    
    	Cmd.AorB = D_B;
    	fillArr(Cmd.cmdArr, "OPEN_CONNECTION_ACKED", sizeof("OPEN_CONNECTION_ACKED"));
    	handleCmd();
    
    	Cmd.AorB = D_A;
    	fillArr(Cmd.cmdArr, "LIST", sizeof("LIST"));
    	handleCmd();
    
    	Cmd.AorB = D_B;
    	fillArr(Cmd.cmdArr, "LIST 6575 LIST", sizeof("LIST 6575 LIST"));
    	handleCmd();
    
    	printf("YES");
    	getchar();
    	return 0;
    }
    hope it will help you if an interviewer asks you about something similar...
    cheers!

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    I don't think this is useful.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    21
    hi bokiboaz,
    what exactly that "stateMachine.h" file contains?could you give brief information about that header file...
    i think this state machine code will be very helpful for me so please...

    thanks and regards,
    Sri

Popular pages Recent additions subscribe to a feed