So, there's a technique used by some called "Profiling." In short, "the act or process of extrapolating information about a person based on known traits or tendencies." (Source) The process usually requires some degree of Perceptiveness, Insight, and general Wisdom to be able to piece together from characteristics seen what the person may do and extrapolating from that what Class they may be. While Profiling is an art as much as it is science, you should also come up with alternative possibilities in case your first guess is wrong.
To a degree, what you're looking at is a character like the BAU in Criminal Minds, Sherlock Holmes in (well) Sherlock, or Patrick Jane from The Mentalist and you would want your character to make use of the tricks seen in these shows.
This gets you from a real-world perspective, but now we need to translate that into D&D 5E. Of course, you can ask various questions to your DM in order to try and pull information and prompt rolls, but this may seem inefficient. "What are they wearing specifically that I can see?" DM says, but doesn't mention footwear "Are they barefoot or...?" DM answers, slowly getting annoyed "Do they have any weapons on them?" Yes/No "Are there any noticeable scars or calluses on their hands?" Roll Perception: Yes, a scar in between his index and middle finger/No/You can't see the hand clearly enough (If Yes:) "Oh! He's probably an archer!" (Otherwise: "What is his stance and posture?" ad nauseam That helps at least. Combine that with probing questions combined with Detect Thoughts, you could easily exfiltrate information about them that they wouldn't normally give away so easily.
Courtesy of Roll20
Questions verbally directed at the target creature naturally shape the course of its thoughts, so this spell is particularly effective as part of an interrogation.
Even if it's just a casual conversation, you can still learn this information, if you're skilled enough to ask the right things and if you or your character are wise enough to puzzle together the pieces you're given.
Combine this with the information you can gather from being a Battle Master Fighter (as was earlier mentioned) and combine it with the persuasiveness of high Charisma, you could effectively learn the general aspects of the character, even if you may be missing chunks of information. Even if you do fail to learn the specific class build they have, you can still learn a lot of other useful information about them that it should make up for it.
That said, never assume the DM will cooperate with you as you do this. They don't have to play along. They can limit the information you get or punish you for being too nosy. Also, this hardly constitutes as RAW. While a lot of it can be expected to work (like Battle Master), it's still in enough ways interpretation of what 5e allows that you shouldn't take it for granted if your GM says "no".
It's also worth noting, if you want this to be effective your character has to be MAD (multiple ability-scores dependent). It's not that it can't work if your ability scores are a little low, but the higher your Wisdom and Charisma, the better, and if you're a fighter, you want high Strength too. It's not an optimal scenario.