Is it possible for a finite state machine to detect if a string has the same number of ones as zeros?

The number of $1$'s in the first $n$ bits of input may be any integer from $0$ to $n$, and if there are at least $n$ more bits to go, in order to determine the result the FSM would need to "remember" which of those integers it is. Thus at least $n+1$ states are needed for a FSM that can do this with $2n$-bit strings. Since the size of the input string is arbitrary, no such FSM is possible.


This is clearly possible with Turing machines, so let me assume that you are using finite state automata.

In this case, it is not possible. The reason is the pumping lemma. Consider the strings $0^n1^n$, where $n$ is larger than the number of states in the machine. This string must be accepted by the machine, and so it can be pumped. But by the pumping lemma, the effect of pumping will be to insert extra $0$s into the string, making it unbalanced with respect to the criterion, but still accepted by the machine. So the machine does not work as suggested. So there is no finite state automata accepting all and only the balanced strings.