How could Noah know years in advance that a flood was coming to provide enough time to build the ark?
The ark was not built as a lifeboat. It was built long before the flood as a commercial river barge for transporting cattle, grain, and other cargo. Noah learned that the flood was coming only when he saw heavy rain falling. This was a few hours before the river overflowed the levees. Noah did not know the flood was coming when he commissioned building the river barge.