Imagine we could scale up noise cancellation technology in earphones and apply it to earthquake prone zone, what challenges beside engineering and cost would make such a machine impossible to realise? If we could somehow mitigate or minimise the shaking so that the buildings can never hit resonance freq and thus would not collapse, is such technology too far fetch?
what challenges beside engineering and cost would make such a machine impossible to realise?
The same reason for which you have noise cancelling earphones but not noise cancelling speakers.
Earthquakes produce different types of seismic waves, like P-wave, S-wave and many more, each traveling with a different velocity.
If you want to nullify the disruptive effects of the earthquake in a certain location, you will have to produce waves of equal amplitude and opposite phase in that location. But then those waves would travel across the crust, finding nothing to nullify them. You will end up nullifying the damage in a limited area but doing more damages all around it.
This technology, called "Tuned Mass Dampers", is in practical use for decades, see the corresponding Wikipedia article. In skyscrapers, they do exactly what is asked for: changing the resonant frequency of a building (by means of a pendulum, for example) in a way that it never resonates to the frequency typical for an earthquake.
In regard to earthquakes, the article states:
The seismic waves caused by an earthquake will make buildings sway and oscillate in various ways depending on the frequency and direction of ground motion, and the height and construction of the building. Seismic activity can cause excessive oscillations of the building which may lead to structural failure. To enhance the building's seismic performance, a proper building design is performed engaging various seismic vibration control technologies. As mentioned above, damping devices had been used in the aeronautics and automobile industries long before they were standard in mitigating seismic damage to buildings. In fact, the first specialized damping devices for earthquakes were not developed until late in 1950.
A small-scale experimental setup is described (including video) by the Practical Engineer.
EDIT: If you need/want an active setup (in the sense that some sort of controller predicts the next movement and actively tries to cancel the shaking using some sort of actuator, like in noise cancelling), I imagine that you could just extend the pendulum variant of a Tuned Mass Damper with really strong motors that react to an array of seismic sensors around the building (assuming P-waves in granite at 5000m/s, they need to be in about 5 km distance if the sensors, communication line and the motors together have a reaction time of 1 second). This would minimise the shaking further, but a misinterpretation of the sensors then would induce active shaking.
You can't cancel the stress
L.Dutch provides a good answer as to why your specific technology doesn't work. But more fundamentally, there's a bigger problem with any sort of reactionary earthquake prevention tech.
The shifting of Earth's crust on the mantle causes buildup of enormous stresses. An earthquake is what you get when a bunch of those stresses release all at once. If you could push a button and "cancel" that earthquake, then those stresses would still be there, and soon you'll have another earthquake on your hands, probably even bigger than the one you prevented. Or, as L.Dutch described, you might prevent the effects of the quake in one area and get increased damage in other areas.
In theory, a technology could be created that would bleed off the stresses gradually so as to prevent them from snapping all at once - effectively replacing single large earthquakes with thousands of micro quakes. But it would be a very different technology than the one you describe.
Incidentally, there is a related concept in forest management, where small natural fires are allowed to burn freely to prevent the proliferation of undergrowth that could fuel unstoppable firestorms.
Even if it works, consider that if this were to work in a certain area where the local waves are perfectly phased to cancel out, other nearby areas could experience constructive interference. Other neighborhoods might not appreciate that ;)
Constructive vs destructive interference in 2D space
To prevent buildings from experiencing shaking due to an earthquake, Its not necessary to cancel the waves (active isolation). A much easier (totally passive) approach is to put the buildings on a giant shock isolator which has a lot of attenuation at the typical frequencies produced by earthquakes. Effectively you would be shifting the resonance frequency of the system to avoid damage.
This basically amounts to putting the buildings on some sort of stiff spring, and using the combination of the spring and the buildings mass to attenuate the incoming shaking.