Orthogonal matrix which preserves the eigenvalues of a diagonal matrix This is a follow-up of this question.
Let $A$ be a real diagonal $n \times n$ matrix, with rank $\ge n-1$.

Suppose that the eigenvalues (counted with multiplicities) of $A$ are the same as the eigenvalues of $QA$ for some special orthogonal matrix $Q$. Must $Q$ be diagonal?

The condition $\text{rank}(A)\ge n-1$ is necessary: If we allow $\text{rank}(A)< n-1$, then one can take $A$ to be diagonal with the $A_{11}=A_{22}=0$; then the entire $\text{SO}(2) \times \text{Id}_{n-2}$ preserves the eigenvalues.
 A: No, for example $Q$ can be any rotation and $A=\begin{pmatrix}1&0\\0&-1\end{pmatrix}.$
I think this is pretty much the general case: $\mathbb R^n$ splits as a direct sum of some number of copies of the vector spaces $E_a\simeq\mathbb R^1$ and $V_{a,\theta}\simeq\mathbb R^2$ where:


*

*$Q$ acts as $1$ on $E_a,$ and $A$ acts as $a$

*$Q$ acts as the rotation $\begin{pmatrix}\cos\theta&\sin\theta\\-\sin\theta&\cos\theta\end{pmatrix}$ on $V_{a,\theta},$ and $A$ acts as $\begin{pmatrix}a&0\\0&-a\end{pmatrix}$
I will just show how you can reduce the problem to analyzing direct sums of certain two-dimensional spaces - this isn't a complete proof of the above characterization.
The important equation is:
$$QA=AQ^T\tag{1}$$
This can be seen in two ways:


*

*Working over $\mathbb C$ temporarily, Schur triangulization gives a unitary $U$ and upper triangular $M$ such that $QA=U^*MU.$ Since $M$ and $QA$ are similar, they have the same characteristic polynomial. So the diagonal entries of $M$ are just the eigenvalues of $QA,$ which are the same as the diagonal entries of $A$ by assumption. So $\sum_i |M_{ii}|^2=\sum_i A_{ii}^2=\|A\|_F^2$ (squared Frobenius norm). But $M$ and $A$ have the same Frobenius norm, so $\sum_{ij} |M_{ij}|^2=\sum_i |M_{ii}|^2,$ forcing the off-diagonal entries to all be zero. So $M$ is diagonal, but the diagonal entries are real so it's in fact Hermitian. So $QA$ is Hermitian, giving $QA=(QA)^*=A^*Q^*=AQ^T,$ which is (1).

*Since $QA$ and $A$ have the same eigenvalues $\lambda_1,\dots,\lambda_n\in\mathbb R,$ the traces $\operatorname{tr}(A^2)$ and $\operatorname{tr}((QA)^2)$ are both $\sum_i\lambda_i^2.$
Using Cauchy-Schwarz on the Frobenius inner product gives $\operatorname{tr}(QAQA)\leq \|QAQ\|_F\|A\|_F=\|A\|_F^2=\operatorname{tr}(A^2)$ with equality only when $QAQ=A^T=A,$ and multiplying on the right by $Q^T$ gives (1).
Equation (1) is the defining relation of the infinite dihedral group, except that $A$ is not required to be invertible. The representation theory will be similar. Multiplying (1) on the left by $Q^T$ and on the right by $Q$ gives
$$Q^TA=AQ\tag{2}.$$
If we have a subspace $U\subseteq V$ invariant under $Q$ and $A,$ then the orthogonal subspace $U^\perp$ is also invariant under $Q$ and $A.$ Indeed $U^\perp$ is automatically invariant under the adjoints $Q^T$ and $A^T=A$ because $\langle Aw,v\rangle=\langle w,Av\rangle=0$ and $\langle Q^Tw,v\rangle=\langle w,Qv\rangle=0,$ and being invariant under $Q$ is the same as being invariant under $Q^T.$ So it makes sense to decompose $V$ into the direct sum of subspaces that are invariant under $Q$ and $A,$ and that cannot be decomposed further i.e. irreducible representations. Though we can't use the eigenvalues condition directly when analyzing these subspaces.
(1) and (2) imply that the matrices $A$ and $Q+Q^T$ commute. Let $v$ be a simultaneous eigenvector of $A$ and $Q+Q^T.$
So $Av=av$ and $(Q+Q^T)v=\lambda v$ for some scalars $a$ and $\lambda.$
Then $\operatorname{span}\{v,Q^Tv\}$ is invariant under $Q$ and $A$ because $Qv=\lambda v-Q^Tv$ and $AQ^Tv=QAv=aQv$ (and we already took care of $Qv$).
So irreducible representations have dimension at most two.
To finish a characterization you could analyze these two-dimensional subspaces.
A: This is not true. 
Note this is very easy and true when $A \succeq 0$ (with $rank(A) \geq n-1$ this comes down to the triangle inequality).  It is also easily true when all $\big \vert \lambda_i\big \vert$ are distinct.  
for a counter example, consider, e.g. n=4, and any $\lambda \gt 0$
$P := \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}$
$Q := \begin{bmatrix} P & \mathbf 0 \\ \mathbf 0 & P \end{bmatrix}$
and
$A := \begin{bmatrix}\lambda I_2 & \mathbf 0 \\ \mathbf 0 & -\lambda I_2 \end{bmatrix}$ 
then 
$QA = \begin{bmatrix} P & \mathbf 0 \\ \mathbf 0 & P \end{bmatrix}\begin{bmatrix}\lambda I_2 & \mathbf 0 \\ \mathbf 0 & -\lambda I_2 \end{bmatrix} = \begin{bmatrix} \lambda P & \mathbf 0 \\ \mathbf 0 & -\lambda P \end{bmatrix}$ 
$Q$, however is not diagonal, but $\det\big(Q\big)=\det\big(P\big)^2 = 1$ and $Q^TQ = I$ 
$QA$ has $\lambda$ with algebraic (and geometric) multiplicity of 2 and $-\lambda$ with the same multiplicity, just like in $A$.  
edit to address the two sufficient conditions asked for in the comment:
with $\mathbf e_k$ denoting the kth standard basis vector  
sufficient case 1:
where $A\succeq 0$ and is positive on all diagonal components except $\lambda_n=a_{n,n}=0$, we have the point-wise bound, for $k \in \{1,2,..., n-1\}$ 
$\lambda_k \cdot q_{k,k} \leq \vert \lambda_i \cdot q_{k,k}  \vert = \lambda_k \cdot\vert  q_{k,k} \vert  \leq \lambda_k$
with equality iff $q_{k,k}= 1$, and recalling that since each column of $\mathbf Q$ is mutually orthonormal, each component has modulus at most 1, 
i.e. $q_{k,k}^2 \leq q_{k,k}^2 + \sum_{i\neq k}q_{i,k}^2 = \big \Vert \mathbf q_k\big \Vert_2^2= 1$, then take square roots to get the claim.
Equality is obtained iff all off diagonal components are zero in column k.  
Summing over the point-wise bound we have  
$\text{trace}\big(QA\big) = \big \vert\text{trace}\big(QA\big)\big \vert =\big\vert\sum_{k=1}^{n-1} \lambda_k \cdot q_{k,k} \big\vert \leq \sum_{k=1}^{n-1} \lambda_k \big\vert q_{k,k} \big\vert  \leq \sum_{k=1}^{n-1} \lambda_k  =  \text{trace}\big(A\big)$
but this is met with equality so we know that each $q_{k,k}=1$ i.e. $\mathbf q_k = \mathbf e_k$.  The only remaining mutually orthogonal vector implies $\mathbf q_n \propto \mathbf e_n$.  In the simpler case where $A\succ 0$ -- i.e. $\lambda_n \gt 0$ then the above argument can be re-run and we actually know $Q =I$ 
The idea behind this is a common trace inequality
$\big \vert\text{trace}\big(VB\big)\big \vert \leq \text{trace}\big(B\big)$ for any hermitian positive (semi) definite $B$ and unitary $V$.  It's provable many different ways.  Geometrically it ties in with interpretation of Polar Decomposition.  It also gives an easy proof of subadditivity of the Schatten 1 norm (nuclear norm).  
sufficient case 2:
where each $\big \vert \lambda_i \big \vert$ is distinct (or equivalently each $\sigma_i$ is distinct).  With the ordering $\big \vert \lambda_1 \big \vert \gt \big \vert \lambda_2 \big \vert \gt ... \gt \big \vert \lambda_n \big \vert$ 
Since $QA$ has the same eigenvalues as $A$, and multiplication by an orthogonal matrix doesn't change singular values, then $QA$ has the same singular values as $A$.  This implies that $QA$ is normal.  The standard approach is noting it meets the inequality of Schur with equality, i.e.  
$\text{trace}\big(A^2\big) = \big \Vert QA \big \Vert_F^2 = \sum_{i=1}^n \sigma_i^2 \geq \sum_{i=1} \big \vert \lambda_i\big \vert^2 = \text{trace}\big(A^2\big)$
normality is also implied if you repeatedly apply this refinement I recently did:
Proving that left and right eigenvector are equal using singular values 
since $QA$ is normal, it commutes with its conjugate transpose, which is its transpose since the matrix is real, so
$QA^2Q^T =QAA^TQ^T = (QA)(QA)^T =  (QA)^T(QA) =  A^T Q^T Q A = A^2$ 
so
$QA^2Q^T = A^2$
all eigenvalues of $A^2$ are distinct (i.e. each $\lambda_i^2$ is distinct because each $\big \vert \lambda_i \big \vert$ is distinct), which means each eigenspace has dimension one -- i.e. up to rescaling each $\lambda_i^2$ has a single unique eigenvector associated with it.  By inspection these are standard basis vector for diagonal matrix $A^2$. Looking at the eigenvalue equation, 
$A^2Q = QA^2  $  (since $A^2$ is diagonal, this is merely collecting $A^2 \mathbf q_k = \lambda_k^2 \mathbf q_k$ for each eigenvector)  
which tells us that each $\mathbf q_k \propto \mathbf e_k$, so $Q$ must be diagonal  
