DMG Blockchain Interview Question for Blockchain Developers


Country: Canada
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
0
of 0 vote

//remove duplicates from result

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

#define MAX_ROW 4
#define MAX_COLUMN 3

struct ArrayIndex
{
    int rowIndex;
    int columnIndex;
    int direction;
   
};

void getNextIndex(struct ArrayIndex* obj,vector<struct ArrayIndex> *IntermediateIndex,int directionIncluded)
{
    int rowIndex =  obj->rowIndex;
    int columnIndex = obj->columnIndex;
    int direction = obj->direction;
    struct ArrayIndex IntermediatePos;
    if(directionIncluded)
    {
        if(direction)
        {
            //change columnIndex
            if ((0<= (columnIndex + 1) )&&((columnIndex + 1)<MAX_COLUMN ))
            {
                IntermediatePos.rowIndex = rowIndex;
                IntermediatePos.columnIndex= columnIndex +1;
                IntermediatePos.direction= 0;
                IntermediateIndex->push_back(IntermediatePos);
            }  
            if ((0<= (columnIndex - 1) )&&((columnIndex - 1)<MAX_COLUMN ))
            {
                IntermediatePos.rowIndex = rowIndex;
                IntermediatePos.columnIndex= columnIndex - 1;
                IntermediatePos.direction= 0;
                IntermediateIndex->push_back(IntermediatePos);
            }  
        }
        else
        {
            //change rowIndex
            if ((0<= (rowIndex + 1) )&&((rowIndex + 1)<MAX_ROW ))
            {
                IntermediatePos.rowIndex = rowIndex+1;
                IntermediatePos.columnIndex= columnIndex;
                IntermediatePos.direction= 1;
                IntermediateIndex->push_back(IntermediatePos);
            }  
            if ((0<= (rowIndex - 1) )&&((rowIndex - 1)<MAX_ROW ))
            {
                IntermediatePos.rowIndex = rowIndex-1;
                IntermediatePos.columnIndex= columnIndex;
                IntermediatePos.direction= 1;
                IntermediateIndex->push_back(IntermediatePos);
            }  
        } 
   }
   else
   {

        if ((0<= (rowIndex + 1) )&&((rowIndex + 1)<MAX_ROW ))
        {
            IntermediatePos.rowIndex = rowIndex+1;
            IntermediatePos.columnIndex= columnIndex;
            IntermediatePos.direction= 1;
            IntermediateIndex->push_back(IntermediatePos);
        }  
        if ((0<= (rowIndex - 1) )&&((rowIndex - 1)<MAX_ROW ))
        {
            IntermediatePos.rowIndex = rowIndex-1;
            IntermediatePos.columnIndex= columnIndex;
            IntermediatePos.direction= 1;
            IntermediateIndex->push_back(IntermediatePos);
        }  
        if ((0<= (columnIndex + 1) )&&((columnIndex + 1)<MAX_COLUMN ))
        {
            IntermediatePos.rowIndex = rowIndex;
            IntermediatePos.columnIndex= columnIndex +1;
            IntermediatePos.direction= 0;
            IntermediateIndex->push_back(IntermediatePos);
        }  
        if ((0<= (columnIndex - 1) )&&((columnIndex - 1)<MAX_COLUMN ))
        {
            IntermediatePos.rowIndex = rowIndex;
            IntermediatePos.columnIndex= columnIndex - 1;
            IntermediatePos.direction= 0;
            IntermediateIndex->push_back(IntermediatePos);
        }  
        //for(int i=0;i<IntermediateIndex->size();i++) cout<<IntermediateIndex[0][i].rowIndex<<"+"<<IntermediateIndex[0][i].columnIndex;
   }
}

void getNextRowColumn2(vector<ArrayIndex>*  IntermediateIndex, int depth, int directionIncluded)
{

    getNextIndex(&(IntermediateIndex[0][0]),&IntermediateIndex[1],directionIncluded);
    for(int i=0;i<IntermediateIndex[1].size();i++)
    {
        directionIncluded = 1;
        getNextIndex(&(IntermediateIndex[1][i]),&IntermediateIndex[2],directionIncluded);
    }
    IntermediateIndex[0].erase(IntermediateIndex[0].begin());
}
void getNextRowColumn(vector<ArrayIndex>*  IntermediateIndex, int depth, int directionIncluded)
{
    if(depth == 0)
    {
        for(int i=0;i<IntermediateIndex[0].size();i++)
        {
            getNextIndex(&(IntermediateIndex[0][i]),&IntermediateIndex[1],directionIncluded);
            //getNextIndex(&(IntermediateIndex[1][i]),&IntermediateIndex[2],directionIncluded);
            //IntermediateIndex[0].erase(IntermediateIndex[0].begin());
            
        }
    }
    else
    {
        for(int i=0;i<IntermediateIndex[1].size();i++)
        {
            getNextIndex(&(IntermediateIndex[1][i]),&IntermediateIndex[2],directionIncluded);
        }
    }
}
void getArrIndexFromNumber(int*rowIndex,int*columnIndex,int digit)
{
    switch(digit)
    {
        case 0:
               *rowIndex = 3;
               *columnIndex = 1;
               break;
        case 1:
               *rowIndex = 0;
               *columnIndex = 0;
               break;
        case 2:
               *rowIndex = 0;
               *columnIndex = 1;
               break;
        case 3:
               *rowIndex = 0;
               *columnIndex = 2;
               break;
        case 4:
               *rowIndex = 1;
               *columnIndex = 0;
               break;
        case 5:
               *rowIndex = 1;
               *columnIndex = 1;
               break;
        case 6:
               *rowIndex = 1;
               *columnIndex = 2;
               break;
        case 7:
               *rowIndex = 2;
               *columnIndex = 0;
               break;
        case 8:
               *rowIndex = 2;
               *columnIndex = 1;
               break;
        case 9:
               *rowIndex = 2;
               *columnIndex = 2;
               break;
    }
}

void qualifyNum(int TelKey[4][3],vector<int>& prevQualifyingNumberArray,int digits,vector<int>& qualifyingNumberArrayResult)
{
    int number = 0,tempNumber; 
    int rowIndex, columnIndex, lastDigit;
    struct ArrayIndex IntermediatePos;  
    vector<struct ArrayIndex> IntermediateIndex[3];
    //vector<int> prevQualifyingNumberArrayResult;    
    if(prevQualifyingNumberArray.size() == 0)
    {
        qualifyingNumberArrayResult.push_back(number);
        return;   
        //return prevQualifyingNumberArrayResult;
    }
    else if(prevQualifyingNumberArray.size() == 1)
    {
        getArrIndexFromNumber(&rowIndex,&columnIndex,0);        
        if ((0<= (rowIndex + 2) )&&((rowIndex + 2)<MAX_ROW ))
        {
            IntermediatePos.rowIndex = rowIndex+2;
            IntermediatePos.columnIndex= columnIndex;
            IntermediatePos.direction= 1;
            IntermediateIndex[0].push_back(IntermediatePos);
        }  
        if ((0<= (rowIndex - 2) )&&((rowIndex - 2)<MAX_ROW ))
        {
            IntermediatePos.rowIndex = rowIndex-2;
            IntermediatePos.columnIndex= columnIndex;
            IntermediatePos.direction= 1;
            IntermediateIndex[0].push_back(IntermediatePos);
        }  
        if ((0<= (columnIndex + 2) )&&((columnIndex + 2)<MAX_COLUMN ))
        {
            IntermediatePos.rowIndex = rowIndex;
            IntermediatePos.columnIndex= columnIndex +2;
            IntermediatePos.direction= 0;
            IntermediateIndex[0].push_back(IntermediatePos);
        }  
        if ((0<= (rowIndex + 2) )&&((rowIndex + 2)<MAX_COLUMN ))
        {
            IntermediatePos.rowIndex = rowIndex;
            IntermediatePos.columnIndex= columnIndex - 2;
            IntermediatePos.direction= 0;
            IntermediateIndex[0].push_back(IntermediatePos);
        }  
        //for(int i=0;i<IntermediateIndex[0].size();i++) cout<<IntermediateIndex[0][i].rowIndex<<"+"<<IntermediateIndex[0][i].columnIndex;
        //cout<<endl;
        
        getNextRowColumn(IntermediateIndex,0,1);
        for(int i=0;i<IntermediateIndex[1].size();i++)
        {
            qualifyingNumberArrayResult.push_back(TelKey[IntermediateIndex[1][i].rowIndex][IntermediateIndex[1][i].columnIndex]);
        }    
            IntermediateIndex[0].clear();
            IntermediateIndex[1].clear();
        return;
    } 
    else
    {
        for(int i=0;i<prevQualifyingNumberArray.size();i++)
        {
            number = prevQualifyingNumberArray[i];
            lastDigit = number % 10;
            getArrIndexFromNumber(&rowIndex,&columnIndex,lastDigit);
            IntermediatePos.rowIndex = rowIndex;
            IntermediatePos.columnIndex= columnIndex ;
            IntermediatePos.direction = -1 ;
            IntermediateIndex[0].push_back(IntermediatePos);
            while(IntermediateIndex[0].size())
            {   
                getNextRowColumn2(IntermediateIndex, 0, 0);
                //getNextRowColumn(IntermediateIndex, 1, 0);
                for(int j=0;j<IntermediateIndex[2].size();j++)
                {
                    tempNumber = number*10+TelKey[IntermediateIndex[2][j].rowIndex][IntermediateIndex[2][j].columnIndex];
                    qualifyingNumberArrayResult.push_back(tempNumber);
                }
                IntermediateIndex[1].clear();
            }
            IntermediateIndex[0].clear();
            IntermediateIndex[1].clear();
            IntermediateIndex[2].clear();
        }
        return;
    }

}

int main()
{
    int TelKey[4][3]={1,2,3,\
                      4,5,6,\
                      7,8,9,\
                     '*',0,'#'};
    vector<int> prevQualifyingNumberArray[100];
    prevQualifyingNumberArray[0].reserve(0);
    for(int digits=1; digits<=3;digits++)
    { 
        qualifyNum(TelKey,prevQualifyingNumberArray[digits-1],digits,prevQualifyingNumberArray[digits]);
        for(int i= 0; i<prevQualifyingNumberArray[digits].size(); i++) cout<<prevQualifyingNumberArray[digits][i]<<";";
        cout<<endl;
    }
}

- mohapatrasandeep60 June 18, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi can you please explain your code and also it does not work for 4 digits and above .

- Randomguy December 12, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More