A1398 is used for a range of MacBook Pro 15” retina models from 2012 to 2015. All models are capable of on El Capitan, High Sierra, and Mojave, and variously other macOS releases, and given sufficient memory and available storage on this specific MacBook Pro.
Rather than Yosemite, download El Capitan (or later) to and then create a bootable USB installer (with related instructions) on another Mac, and then transfer that USB installer to the target iMac.
Boot that USB installer on the target Mac, and use Disk Utility to wipe and partition as GPT and format the internal storage as HFS+, and then use that installer to install El Capitan, High Sierra, or Mojave on the target iMac.
Some addenda...
It’s fairly common for folks using a previously-owned Mac to have not gone through the ‘Welcome to Mac” installation and set-up sequence when first starting out with the Mac, and which can mean the Apple ID and the install and the software are not associated with your current Apple ID; they’re associated with the previous owner’s Apple ID. This causes various issues with access to older releases, and with access to software updates. The option here is to back up, wipe, reinstall, gomthrough the “welcome”, and migrate in your files and apps using Migration Assistant.
It’s also fairly common for folks doing macOS reinstalls on older hardware to be dealing with a nascent hardware problem. Failing hard disk drives can manifest with corruptions, beachballs, performance degradation, wedges and crashes. Among other symptoms.
Apple is making it more difficult to acquire and to stay on older releases.