Interview-Example of Model View Controller In Real World

This is another simple question which was asked to me in an interview.
As all interviewer requires real world example of every concept so the MVC (Model View Controller) application can be seen in a Calculator.

The buttons is the Controller.-> when you press the add button request is send to model.
The back end hidden knowledge is Model-> When you press the add button some business logic runs let say two number are added in the calculator then the answer is send to view.

The Display is View->The View is responsible to display the result.

mvcdiagram

Interview-How To Store Directory Structure In Database

This was asked in an interview question that what will be the data structure schema of database if you  are given to save folder and they have files stored in it and also they contain sub folders in it.

So the best schema for this type of problem is that we will only require one table.
Yes one Table will do the job and more interestingly only 3 fields are enough to save  this information.

Field Name      –   Key                    – Data Type – Nullable

1). Dir                 – Primary Key   -int                   – Not Null
2). Dir_Parent –  Foreign Key   -int                   – Null Allowed
3). Description – Null                 -Varchar          – Not Null

dirdbschema

Here on top is the description of how data will be save in database using one table and .

Array Syntax Acceptance Flex Bison

Flex File

%{
#include <stdio.h>
#include <stdlib.h>
#include “cal.tab.h”
%}
%%

“int” return INT;
“char” return CHAR;

[,;()\n] return *yytext;
[a-zA-Z]+ {yylval=*yytext;
return VARIABLE;}
[0-9]+ {yylval = atoi(yytext);
return NUM;}
“{” { return ‘{‘; }
“}” { return ‘}’; }
“[” { return ‘[‘; }
“]” { return ‘]’; }
“=” { return ‘=’; }
%%

int yywrap(void) {
return 1;
}

Bison

%{
#include <stdio.h>
#include <string.h>

void yyerror(const char *str)
{
fprintf(stderr,”error: %s\n”,str);
}

 

%}

%token INT
%token VARIABLE
%token CHAR NUM
%%
program : program statement ‘\n’
|
;

statement : INT VARIABLE ‘[‘NUM’]’ ‘;’ {printf(“ACCEPT\n”);}
| CHAR VARIABLE ‘[‘NUM’]’ ‘;’ {printf(“ACCEPT\n”);}
| INT VARIABLE ‘[‘NUM’]’ ‘=’ ‘{‘ exp ‘}’ ‘;’ {printf(“ACCEPT\n”);}
| CHAR VARIABLE ‘[‘NUM’]’ ‘=’ ‘{‘ expr ‘}’ ‘;’ {printf(“ACCEPT\n”);}
;

 

exp :NUM
|exp’,’ NUM
;

expr :VARIABLE
|expr ‘,’VARIABLE
;
%%
int main()
{
yyparse();
return 0;
}

 

 

Checking of repetition of inputs and basic items FLEX

Flex File 

%{

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *s;
int i=0 ;
int f=0;
char arr[8][8];

%}

Operator [+|*|/|>|<|=]

Parenthsis [(|)]

Quoted [“][a-zA-Z][a-zA-Z_0-9_?_, ]*[“]

Integers [0-9]+

Identifier [a-zA-Z][a-zA-Z_0-9]*

%%

[ \t\n] ;

int|float|decimal|double|array|list printf( “The sentence contains the special DataType = %s\n”, yytext );

if|then|else|endif|print|newline printf( “The sentence contains the special keyword = %s\n”, yytext );

“;” printf( “The sentence contains the special Semicolon = %s\n”, yytext );

“-” printf( “The sentence contains the special operator = %s\n”, yytext );
“=>” printf( “The sentence contains the special operator = %s\n”, yytext );
“>=” printf( “The sentence contains the special operator = %s\n”, yytext );
“<=” printf( “The sentence contains the special operator = %s\n”, yytext );
“=<” printf( “The sentence contains the special operator = %s\n”, yytext );
“==” printf( “The sentence contains the special operator = %s\n”, yytext );
“!=” printf( “The sentence contains the special operator = %s\n”, yytext );

{Parenthsis} printf( “The sentence contains the special Parenthesis = %s\n”, yytext );

{Quoted} s=yytext; s++; s[strlen(s)-1] =”; printf( “The sentence contains the special quote = %s \n”,s);

{Identifier} i=0; for(i;i<8;i++) {if(strcmp(arr[i], yytext)==0){printf(” Identifier Already Exsist = %s \n “,yytext);i=1;break;}} if(i>1){printf(“The sentence contains the special Identifier = %s \n”,yytext);strcpy(arr[f],yytext);++f;};

{Integers} printf(“The sentence contains the special integer = %d \n”,atoi(yytext));

 

{Operator} printf(“The sentence contains the special operator = %s\n”,yytext);

 

 

 

%%

int main(int argc,char* argv[])
{
yyin=fopen(argv[1],”r”);
yylex();

 

 

}

 

 

Try Catch (Fariz)

%{

#include “ccproject.tab.h”
#include<stdlib.h>

%}

%%

[0-9]+ {
yylval=atoi(yytext);
return NUM;
}

“try” { return TRY;}

“int” {return KEYWORD;}

“string” {return KEYWORD;}

“float” {return KEYWORD;}

“catch” { return CATCH;}

“exception” { return KEYWORD;}

“printf” { return KEYWORD;}

“Console.Write” {return KEYWORD;}

“.ToString()” {return CONVERT;}
[a-zA-Z]+ { yylval = *yytext;
return ID;
}
“{” { return OPENBRACE; }

“}” { return CLOSEBRACE;}

” ” { return SPACE;}

[()=;\n] return *yytext;

[\t]

. yyerror (“INVALID INPUT”);

%%
int yywrap(void)
{

return 1;

}

Bison

%{

#include<stdio.h>
void yyerror(char*s);

%}

%token NUM TRY ID CATCH KEYWORD OPENBRACE CLOSEBRACE OPENPAREN CLOSEPAREN SPACE CONVERT

%%

program:
program Statement’\n’
|
;

Statement:project {printf(“SYNTAX ACCEPTED\n”);}
project : TRY ‘\n’ OPENBRACE ‘\n’ E1′;’ ‘\n’ E1′;’ ‘\n’ CLOSEBRACE’\n’ catch_statement ‘\n’ OPENBRACE ‘\n’ E3′(‘ ID CONVERT ‘)”;’ ‘\n’ CLOSEBRACE

E1 : KEYWORD SPACE ID’=’NUM
| KEYWORD SPACE ID’=’ID
| ID
| KEYWORD'(‘ID CONVERT’)’
| NUM
;

E3 :KEYWORD
;
catch_statement : CATCH ‘(‘ type_expression’)’
;
type_expression :KEYWORD SPACE ID
;

%%

void yyerror(char*s) {

printf(“%s\n”,s);

}

int main(void) {

yyparse();
return 1;

}

 

Nested If flex bison

Flex File

%{
#include “if.tab.h”
#include<stdlib.h>

%}

%%

if {return IF;}

“else if” {return ELSEIF;}

else {return ELSE;}

“=” {return EQUAL;}

“(” {return BRO; }

“)” {return BRC; }

“{” {return CBO; }

“}” {return CBC; }

“;” {return SEMI; }

“>=”|”<=”|”==”|”!=”|”<“|”>”|”++”|”–” {
yylval = *yytext;
return multiop;
}
[0-9]+ {yylval=*yytext;return NUM;}

[a-zA-Z]+ {yylval=*yytext;return ID;}

[ \t] ; /* skip whitespace */
%%

int yywrap(void)

{

return 1;

}

Bison File

%{

#include <stdio.h>
void yyerror(char *s);

%}

%token IF BRO ELSE BRC CBO CBC SEMI EQUAL NUM ID ELSEIF multiop

%%

program: program State ‘\n’
|
;
State: IF BRO ID EQUAL ID BRC CBO E1 CBC ELSE CBO ID SEMI CBC {printf(“Accept \n”);}
;

E1:A
| A E1
;

A:IF BRO ID EQUAL ID BRC CBO ID SEMI CBC
| A B
;

B: ELSE CBO ID SEMI CBC
;

%%
void yyerror(char *s)
{

{printf(“\n Syntax is Wrong”);}

}

int main(void)
{
yyparse();
return 0;
}