数据结构面试问题

tom*_*h13 3 data-structures

我被问到以下问题:您将如何存储下面给出的数据(您将选择哪种数据结构):

A-1-2-3-4-5-6

|

B-7-8-9-10-11

|

C-12-14-15-16-17
Run Code Online (Sandbox Code Playgroud)

我的答案:因为这看起来像一堆列表,其头节点链接在一起.使用两种节点类型,一种是常规节点类型,具有以下定义:

Struct node1
{
int val;
struct node*next;
};
// to store the numerical part of the data

struct node2
{
 int val;
struct node *down;
struct node* next;
};
//this is the heads of each list.. for the alphabet part of the question.
Run Code Online (Sandbox Code Playgroud)

采访者的反应:这是你能想到的最好的数据结构吗?每个节点所需的遍历和内存如何?

我对此的回答:如果我们创建某种哈希表,我们可以更好地遍历.

我向你们提问的同志:我们能做得更好吗?是否有更好的方法来存储此类数据?

我们假设数据是所有数字(甚至是每个头节点的数字)和非连续的数据,可能重复.什么是正确的答案?在C/C++中寻找答案

Cra*_*aig 6

我要问的第一个问题是数据.这似乎是一个简单的案例,在哪里打破一系列连续数字.鉴于我只会存储断点.这些类型的问题旨在测试您提出问题并深入了解潜在问题的能力.