Sample Code

GitHub link - UPDATE WITH LINK TO SOLUTION CODE IN REPO

#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
char* ltrim(char*);
char* rtrim(char*);
int parse_int(char*);
typedef struct SinglyLinkedListNode SinglyLinkedListNode;
typedef struct SinglyLinkedList SinglyLinkedList;
struct SinglyLinkedListNode {
int data;
SinglyLinkedListNode* next;
};
struct SinglyLinkedList {
SinglyLinkedListNode* head;
SinglyLinkedListNode* tail;
};
SinglyLinkedListNode* create_singly_linked_list_node(int node_data) {
SinglyLinkedListNode* node = malloc(sizeof(SinglyLinkedListNode));
node->data = node_data;
node->next = NULL;
return node;
}
void insert_node_into_singly_linked_list(SinglyLinkedList** singly_linked_list, int node_data) {
SinglyLinkedListNode* node = create_singly_linked_list_node(node_data);
if (!(*singly_linked_list)->head) {
(*singly_linked_list)->head = node;
} else {
(*singly_linked_list)->tail->next = node;
}
(*singly_linked_list)->tail = node;
}
void print_singly_linked_list(SinglyLinkedListNode* node, char* sep, FILE* fptr) {
while (node) {
fprintf(fptr, "%d", node->data);
node = node->next;
if (node) {
fprintf(fptr, "%s", sep);
}
}
}
/**********************************************************************************************************************/
// Ignore everything above here
/*
* Complete the 'findMark' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER rank
* 2. INTEGER_SINGLY_LINKED_LIST marks
*/
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*
*/
int findMark(int rank, SinglyLinkedListNode* marks) {
// Creating a pointer to keep track of the start of the linked list
SinglyLinkedListNode *head = marks;
// Counting how many nodes are in the linked list so we know how large to make our array
int n = 0;
while (marks != NULL) {
n++;
marks = marks->next;
}
// Putting all our mark values into an array so we can sort it easier later
marks = head;
int idx = 0;
int marksArray[n];
while (marks != NULL) {
marksArray[idx] = marks->data;
idx++;
marks = marks->next;
}
// Creating a second array with the marked sorted
int sortedMarks[n];
int i = 0;
while (i < n) {
int currMax = 0;
int currMaxIdx = 0;
int j = 0;
while (j < n) {
if (marksArray[j] > currMax) {
currMax = marksArray[j];
currMaxIdx = j;
}
j++;
}
sortedMarks[i] = currMax;
marksArray[currMaxIdx] = -1;
i++;
}
// Returning the mark of the given rank
// Since the marks are sorted, we can just send back the element at the index of the rank
// (minus 1 because arrays start at index 0)
return sortedMarks[rank - 1];
}
// Ignore everything below here
/**********************************************************************************************************************/
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
int n = parse_int(ltrim(rtrim(readline())));
SinglyLinkedList* l = malloc(sizeof(SinglyLinkedList));
l->head = NULL;
l->tail = NULL;
int l_count = parse_int(ltrim(rtrim(readline())));
for (int i = 0; i < l_count; i++) {
int l_item = parse_int(ltrim(rtrim(readline())));
insert_node_into_singly_linked_list(&l, l_item);
}
int result = findMark(n, l->head);
fprintf(fptr, "%d\n", result);
fclose(fptr);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) {
break;
}
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
break;
}
alloc_length <<= 1;
data = realloc(data, alloc_length);
if (!data) {
data = '\0';
break;
}
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
data = realloc(data, data_length);
if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);
if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}
return data;
}
char* ltrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
while (*str != '\0' && isspace(*str)) {
str++;
}
return str;
}
char* rtrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
char* end = str + strlen(str) - 1;
while (end >= str && isspace(*end)) {
end--;
}
*(end + 1) = '\0';
return str;
}
int parse_int(char* str) {
char* endptr;
int value = strtol(str, &endptr, 10);
if (endptr == str || *endptr != '\0') {
exit(EXIT_FAILURE);
}
return value;
}