How can I trigger custom code when some other module is being installed/uninstalled? I am using a third-party module. Whenever it is installed or un-installed, I want to programmatically create/delete some menus.

How can I detect that a module is installed or un-installed?
You can use hook_modules_installed() to react to modules being installed, e.g.

function hook_modules_installed($modules) {
  if (in_array('lousy_module', $modules)) {
    variable_set('lousy_module_conflicting_variable', FALSE);
  }
}


and hook_modules_uninstalled() to react to the opposite, e.g.

function hook_modules_uninstalled($modules) {
  foreach ($modules as $module) {
    db_delete('mymodule_table')
      -&gt;condition('module', $module)
      -&gt;execute();
  }
  mymodule_cache_rebuild();
}