Exam question about hash tables (interpretation of wording) - hashtable

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.

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.

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

Related

Longest path in adjacency matrix in R

Hypothetical scenario to have a descriptive example: I've a model consisting of 10 parts (vertices) to be put together. Each part can be connected to others (edges) as defined by a connection table.
There's a shortest.paths function in igraph. However here the aim is to find a way to calculate the longest path in the adjacency matrix. Resulting in a path using as many parts as possible, ideally all, so no part of the model is left alone in the end. MWE as follows:
library(igraph)
connections <- read.table(text="A B
1 2
1 7
1 9
1 10
2 7
2 9
2 10
3 1
3 7
3 9
3 10
4 1
4 6
4 7
7 5
7 9
7 10
8 9
8 10
9 10", header=TRUE)
adj <- get.adjacency(graph.edgelist(as.matrix(connections), directed=FALSE))
g1 <- graph_from_adjacency_matrix(adj, weighted=TRUE, mode="undirected")
plot(g1)
Edit:
The result should be something like: for instance if the first part of the model is 8 it could be combined with 9 or 10. Let's say 10 is selected next part can be either 1,2,7 or 9. If 9 is selected as next the follow up could be 1,2,3,7 or 8. If then 8 is selected the model would be finished as part 10 is already in use. The question then would be how to find a way/path to put together as many parts as possible, ideally all of them. The latter would be possible only by starting with 6 or 5.
There are cycles in your graphs, and I don't think you have stated that we cannot use the same vertex (part) more than once: and in this case the longest path might be infinitely long as you can traverse the cycle infinitely many times and then proceed to your destination.
As per your edit, I think this is not allowed. You can use dynamic programming for this I hope. You can start with DFS like algorithm and mark all the vertex except starting as unvisited. Then apply recursion to choose maximum between the longest paths from all the possible vertex we can reach (except which are already visited) from that given vertex.
It is an NP-hard problem, so you would have to check all the possible paths!
You can see: https://en.wikipedia.org/wiki/Longest_path_problem . You will have modify the algorithm to work in graphs with cycle by adding, as stated earlier, a flag to tell which vertices are already visited.
Tell me if i get it right, you are trying to find a path, that touch the maximum number of nodes?
If that so this is basically an instance of the Hamiltonian path problem, I would say an easier version of it if you can pass on a node more than 1 time.
You can try to watch that algorithm.
to respect you edit maybe, you can try to see the graphs search algorithms, you can find something here, however be advise that this type of algorithms are quite heavy on the memory complexity side.

Calculate the number of trips in graph traversal

Hello Stack Overflow Community,
I'm attempting to solve this problem:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1040
The problem is to find the best path based on capacity between edges. I get that this can be solved using Dynamic Programming, I'm confused by the example they provide:
According to the problem description, if someone is trying to get 99 people from city 1 to 7, the route should be 1-2-4-7 which I get since the weight of each edge represents the maximum amount of passengers that can go at once. What I don't get is that the description says that it takes at least 5 trips. Where does the 5 come from? 1-2-4-7 is 3 hops, If I take this trip I calculate 4 trips, since 25 is the most limited hop in the route, I would say you need 99/25 or at least 4 trips. Is this a typo, or am I missing something?
Given the first line of the problem statement:
Mr. G. works as a tourist guide.
It is likely that Mr. G must always be present on the bus, thus the equation for the number of trips is:
x = (ceil(x) + number_of_passengers) / best_route
rather than simply:
x = number_of_passengers / best_route
or, for your numbers:
x = (ceil(x) + 99) / 25
Which can be solved with:
x == 4.16 (trips)

Program asked in a online hiring challenge

Given N integers in the form of Ai where 1≤i≤N, the goal is to find the M that minimizes the sum of |M-Ai| and then report that sum.
For example,
Sample Input: 1 2 4 5
Sample Output: 6
Explanation: One of the best M′s you could choose in this case is 3.
So the answer = |1−3|+|2−3|+|4−3|+|5−3| = 6.
The approach I used is sort the given input and take the middle number as M.
But I was not able to solve all the test cases. I am unable to find any other approach for this question. Where did I go wrong?(Please help me this question has been bugging me from the past 2 days.Thanks)
Can M be any real number or must it be an integer?
If there are no constraints on M your algorithm must work fine.
If M must be an integer then you have to choose M among floor(The Middle Number) and ceiling(The Middle Number).
In which language did you code up the algorithm?

Modelling GLM in R with discrete non-binary response

I'm new to R and I would like to model the following in GLM:
A memory retention experiment where I ask each participant a similar question every X days. The question has either a correct or wrong answer, the participant must keep trying until the answer is correct. I want to find the probability of him answering the question correctly in 1 try given the past data of number of tries and the time offset between questions.
I'm following this tutorial to model it:
http://www.theanalysisfactor.com/r-tutorial-glm1/
Here's an example of a part of my table
0 3 -
1 1 2
0 2 1
0 5 4
1 1 2
The first value is a binary value whether he 'passes' or 'fails'. Answering in 1 attempt is pass and more than that is fail.
The second value is the number of attempts. We can see that if it is 1 then the first value is also 1, else, the first value is 0.
The third value is the number of days between the question and the previous question.
Right now I'm modelling it as
first ~ second + third
I was thinking if there is a better way to do it, since the first is directly related to the second value. Something like only using the second and third value, and finding P(second = 1). And eventually I would also like to find P(second = 2) in the future.
Thanks for your help :)

How to calculate the expected cost?

I am not good at probability and I know it's not a coding problem directly. But I wish you would help me with this. While I was solving a computation problem I found this difficulty:
Problem definition:
The Little Elephant from the Zoo of Lviv is going to the Birthday
Party of the Big Hippo tomorrow. Now he wants to prepare a gift for
the Big Hippo. He has N balloons, numbered from 1 to N. The i-th
balloon has the color Ci and it costs Pi dollars. The gift for the Big
Hippo will be any subset (chosen randomly, possibly empty) of the
balloons such that the number of different colors in that subset is at
least M. Help Little Elephant to find the expected cost of the gift.
Input
The first line of the input contains a single integer T - the number
of test cases. T test cases follow. The first line of each test case
contains a pair of integers N and M. The next N lines contain N pairs
of integers Ci and Pi, one pair per line.
Output
In T lines print T real numbers - the answers for the corresponding test cases. Your answer will considered correct if it has at most 10^-6 absolute or relative error.
Example
Input:
2
2 2
1 4
2 7
2 1
1 4
2 7
Output:
11.000000000
7.333333333
So, Here I don't understand why the expected cost of the gift for the second case is 7.333333333, because the expected cost equals Summation[xP(x)] and according to this formula it should be 33/2?
Yes, it is a codechef question. But, I am not asking for the solution or the algorithm( because if I take the algo from other than it would not increase my coding potentiality). I just don't understand their example. And hence, I am not being able to start thinking about the algo.
Please help. Thanks in advance!
There are three possible choices, 1, 2, 1+2, with costs 4, 7 and 11. Each is equally likely, so the expected cost is (4 + 7 + 11) / 3 = 22 / 3 = 7.33333.

Resources