Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

C code:

struct Node {
        int i; 
        struct Node* next;
};
int* visited;
void dfsvisit(struct Node** adjlist, int s, struct Node** phead) {
        visited[s] = 1;
        for(struct Node* tmp = adjlist[s]; tmp != 0; tmp = tmp->next)
                if(visited[tmp->i] == 0)
                        dfsvisit(adjlist, tmp->i, phead);
        visited[s] = 2;
        struct Node* nn = (struct Node*)malloc(sizeof(struct Node));
        nn->i = s;
        nn->next = *phead;
        *phead = nn;
}

struct Node* dfs(struct Node** adjlist, int nv) {
        visited = malloc(sizeof(int) * nv);
        for(int i = 0; i < nv; i++)
                visited[i] = 0;
        struct Node* head = 0;
        for(int i = 0; i < nv; i++)
                if(visited[i] == 0)
                        dfsvisit(adjlist, i, &head);
        return head;
}
struct Node* topological_sort(int CV, int CE, int preq[][2]) {
        // adjacency list representation of graph
        struct Node** adjlist = (struct Node**)malloc(sizeof(struct Node*) * CV);
        for(int i = 0; i < CE; i++) {
                struct Node* nn = (struct Node*)malloc(sizeof(struct Node));
                nn->i = preq[i][1];
                nn->next = adjlist[preq[i][0]];
                adjlist[preq[i][0]] = nn;
        }
        // return ordered list of subject
        return dfs(adjlist, CV);
}

Test Code: (warning: this test code may generate cyclic graph, for correct result please pass your acyclic prerequisites to topological_sort function.)

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

int main(int argc, char** argv) {
        int CV = 10;
        int CE = 10;
        if(argc > 1)
                CV = atoi(argv[1]);
        if(argc > 2)
                CE = atoi(argv[2]);
        char (*courses)[CV][100] = malloc(sizeof(*courses));
        for(int i = 0; i < CV; i++)
                sprintf((*courses)[i], "c-%d", i);
        int (*preq)[CE][2] = malloc(sizeof(*preq));
        srand(time(0));
        for(int i = 0; i < CE; i++) {
                (*preq)[i][0] = rand() % CV;
                (*preq)[i][1] = rand() % CV;
        }
        struct Node* orderedlist = topological_sort(CV, CE, *preq);
        printf("ordered courses names list:");
        for(struct Node* tmp = orderedlist; tmp != 0; tmp = tmp->next) {
                printf("->[%s]", (*courses)[tmp->i]);
        }
        printf("\n");
        return 0;
}

- badboy May 01, 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