How can you tell what key a melody is in if it uses notes found in multiple scales? And if a note is introduced from a different scale than the one it was in, does that change the key? What if it only plays 2 or 3 notes from the new scale? How many notes must a key be to actually make it a key and not just random intervals?
If the multiple scales the notes are derived from are all minor scales with the same tonic (e.g. both D harmonic minor and D melodic minor), then the melody is in that minor key with that tonic (which would be D minor for our example case).
Things get more difficult for other combinations of scales.
If the multiple scales all have the same tonic (e.g. C minor and C major), I'd generally go for the scale/key used at cadences (so if our new example melody uses both E♭ and E♮, but it uses only E♮ near phrase endings, I'd go with C major).
Common practice period harmony can explain away certain notes outside the usual scale/key. For example, the Neapolitan chord is acceptable in common practice period harmony, and it uses the ♭2 scale degree (which is not found in any of the major or minor scales). Thus, any melody that involves ♭2 can probably have its key determined as if that ♭2 were a regular 2 (2nd scale degree) instead.
A single note from another scale might not change the key of the melody it's in, but it often signifies a tonicization. For example, ♯4 is the hallmark of a secondary dominant of V.
The line between tonicizations and modulations (full-blown key changes) can be blurry at times. For example, is the shift to E flat major very early in Jon Schmidt's "Road Trip" a full-blown modulation, or is it merely a tonicization because it moves away from and back to C major in a mere 4 bars?
As for "How many notes must a key be to actually make it a key and not just random intervals?", I'm not as sure about that. I'm fairly certain that any melody that exclusively uses notes from a pentatonic scale is in a key, but that key is more extensive and inclusive than just that pentatonic scale (e.g. I'd probably classify any melody that uses the F major pentatonic scale as being in F major). I've even been convinced that Helix6's "Archetype" is in C sharp minor despite using only the notes C♯, D, F♯, G, and G♯ in its melody. A melody that uses only the notes B and C is probably going to be hard to classify key-wise, though. While 12-tone serialism, which is atonal, is at the other extreme, I'm fairly convinced that I can construct a melody that uses all 12 notes in the chromatic scale yet follows common practice period harmony and is in one key with relatively few tonicizations.
A few tips
As a rule in melodies, you jump from and to harmony/chord notes. You will look at the key signature, you will discern what exactly the relative key is and you will look if the leading tone of that key is raised.
You will also look at the beginning and end and see what chords the piece is written on, if you, for example, see a piece start and end on an A-minor chord then it is unlikely to be in C Major.
You will also look at whatever accidentals you see and consider how this may be a modulation of some sort. If the piece is in C major and you suddenly see an F# resolving to a G, then you must consider that the piece has modulated to the Dominant key.
This question again demonstrates what benefit provides the tonica do-system:
There are only a few melodies that actually change the key (when we mean melody as the tune of a song). Some chromatics or accidentals are elements of predominants which don't imply a change of key - as well as chromatic approaches.
Using the tonica do system you can adapt the names of doremi by identifying a new chord root as the do (or la) but this will not be a change of key event this is a bridge or chorus section. (refrain)
In classical or baroque pieces you could define a section of 8 measures or longer as a modulation into another key. (e.g the inventions, suites and preludes of Bach where the 2nd section is an excursion into the key of the 5th and returns to the tonic. By reading those measures in the tonica-do system you would better understand the progressions anyway by transposing the do to the dominant or parallel key ...) Bach doesn't notate a change of key, even it actually could be considered as one.
A change of key would be clearly identified in a new section or an interlude between to sections.
(Examples for this will be found in
menuets, solos, concertos with variations, rondos, sonatines, sonatas etc.)
There will be other context, or if there isn't, then it doesn't matter what key the melody is in. Obviously, with other notes in the song we can pinpoint a new key. But if there isn't, why do we care what key, say, three notes are in? If your whole song has the notes C, D, and E in it, the notion of a key is pretty useless.