An algorithmic problem for nilpotent groups and rings