关于哈希表的考试题(措辞解释)

发布于 2024-11-17 09:03:11 字数 680 浏览 0 评论 0原文

我对有关哈希表的特定考试问题的措辞感到困惑。根据我的理解,根据解释可能有两种不同的答案。所以我想知道是否有人可以帮助确定哪种理解是正确的。问题如下:

我们有一个大小为 7 的哈希表来存储整数键,哈希函数为 h(x) = x mod 7。如果我们使用线性探测并按顺序插入元素 1, 15, 14, 3, 9, 5 , 27, 一个元素会尝试移动到被占用的位置多少次?

我来分解一下我对这个问题的两种不同理解。首先,每个元素的初始索引为:

1: 1
15:1
14:0
3:3
9:2
5:5
27: 6

第一种解释:
1:插入索引1
15:尝试前往索引 1,但由于碰撞而向左移动到索引 0。碰撞计数 = 1
14:尝试前往索引 0,但由于碰撞而向左移动到索引 6。碰撞计数 = 2
3:插入索引3
9:插入索引2
5:插入索引5
27:尝试转到索引 6,但由于冲突,移动到索引 5,然后移动到空的索引 4。碰撞次数 = 4

答案:4?

第二种解释:
只计算27因为与索引6中的元素发生碰撞而尝试移动到占用的索引5的时间。

答案:1?

哪个答案是正确的?

谢谢。

I was confused about the wording of a particular exam question about hash tables. The way I understand it there could be two different answers depending on the interpretation. So I was wondering if someone could help determine which understanding is correct. The question is below:

We have a hash table of size 7 to store integer keys, with hash function h(x) = x mod 7. If we use linear probing and insert elements in the order 1, 15, 14, 3, 9, 5, 27, how many times will an element try to move to an occupied spot?

I'll break down my two different understandings of this question. First of all the initial indexes of each element would be:

1: 1
15: 1
14: 0
3: 3
9: 2
5: 5
27: 6

First interpretation:
1: is inserted into index 1
15: tries to go to index 1, but due to a collision moves left to index 0. Collision count = 1
14: tries to go to index 0, but due to collision moves left to index 6. Collision count = 2
3: is inserted into index 3
9: is inserted into index 2
5: is inserted into index 5
27: tries to go to index 6, but due to collisions moves to index 5 and then to 4 which is empty. Collision count = 4

Answer: 4?

Second interpretation:
Only count the time when 27 tries to move to the occupied index 5 because of a collision with the element in index 6.

Answer: 1?

Which answer would be correct?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗恋未遂 2024-11-24 09:03:11

措辞很愚蠢。

老师可能想要#1,但我认为#2迂腐地正确,因为一个元素只会尝试移动到占据的位置一次 >,正如指出的那样。在其他情况下,它不会移动到已占用的位置,而是已占用的位置移动到空闲位置。

学校里的测试有点愚蠢——老师(或助教)已经知道他/她想要什么。 “迂腐地正确”和“满足老师的要求”之间有一条界限。 (永远、永远不要屈服于可证明的错误!)

有一件事情从来没有(至少我记得;-)让我在考试或家庭作业中失败,那就是提供一个答案答案的坚实且正确的理由;这可能还包括解释“其他”答案。

教师/环境、曲目、傲慢和成绩(仅举几例)需要平衡。

快乐上学。

The wording is silly.

The teacher arguably wants #1 but I would argue that #2 is pedantically correct because an element will only ever try to move to an occupied spot once, as pointed out. In the other cases it does not move to an occupied spot but rather from an occupied spot to a free spot.

Tests in school are sort of silly -- the teacher (or TA) already knows what he/she wants. There is a line to draw between "being pedantically correct" and "giving the teacher what they want". (Just never, ever give in to the provably wrong!)

One thing that has never (at least that I recall ;-) failed me in a test or homework is providing an answer with a solid -- and correct -- justification for the answer; this may include also explaining the "other" answer.

Teacher/environment, repertoire, hubris and grade (to name a few) need to be balanced.

Happy schooling.

七秒鱼° 2024-11-24 09:03:11

解释1是正确的。和6碰撞就说明6号槽被占用了,那为什么不数呢?

Interpretation 1 is correct. Collision with 6 means that slot 6 is occupied, so why don't you count it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文