Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new empty hash table with chaining.
DSA Rust
let mut table: Vec<Vec<i32>> = Vec::new();
table.[1](Vec::new());Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push removes elements instead of adding.
Using remove or insert requires an index and is not suitable here.
✗ Incorrect
We use push to add a new empty vector (chain) to the hash table vector.
2fill in blank
mediumComplete the code to compute the hash index for a key.
DSA Rust
let index = (key as usize) [1] table.len();
Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) returns a quotient, not an index.
Using multiplication (*) or addition (+) can go out of bounds.
✗ Incorrect
The modulo operator % gives the remainder, which fits the key into the table size.
3fill in blank
hardFix the error in adding a key to the chain at the computed index.
DSA Rust
table[index].[1](key);Attempts:
3 left
💡 Hint
Common Mistakes
Using remove or pop removes elements instead of adding.
Using insert requires an index and is more complex here.
✗ Incorrect
push adds the key to the end of the chain vector at the index.
4fill in blank
hardFill both blanks to check if a key exists in the chain at the index.
DSA Rust
if table[index].[1](|&x| x == key) [2] None { println!("Key found"); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using contains returns a bool directly instead of Option.
Using == None checks if key is NOT found.
✗ Incorrect
iter().position(|&x| x == key) returns an Option which is Some(position) if the key is found, so != None confirms the key exists.
5fill in blank
hardFill all three blanks to remove a key from the chain if it exists.
DSA Rust
if let Some(pos) = table[index].[1](|&x| x == key) { table[index].[2](pos); println!("Removed key: {}", [3]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes the last element, not the key.
Using contains instead of position does not give index.
✗ Incorrect
iter().position finds the position of key; remove(pos) deletes it; printing key shows which key was removed.
