AFAIK it derives tree and scan external chain addresses to see transactions.
Do you know how far does it derive the tree(depth and breath) ?
It stops searching until "X" number of consecutive unused addresses is found, where X is gap limit. By default, gap limit is 20 but it depends on wallet software. I think you can also go with 20 as gap limit.