IBM interview question

Medium level DSA question asked