• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

April 27, 2015 by Dhaval Dave

With First look you think practical way of solving this question, and that is randomly taking a bolt and finding (by Looking at all nuts) perfect fit nut for it.
but does such logic to find approximate fit nut doing hit and try is possible in simple coding practice ?
No , so in order to achieve that what can be simple ways

Method 1) Brute Force Approach

Take a nut and find its pair bolt in all Bolts.
Complexity : O(n^2)

Method 2) Simple and Efficient

Sort all bolts and all nuts based upon diameter ..  All pairs will be automatically formed.
Nuts[i] <-> Bolt[i]. (as same diameter bolt and nut will fit )
But this will be inefficient as question is asked with least comparisons .
here comparisons  will increase (as You are sorting Nuts and Bolts both)
with less sorting complexity also it can be done if you use non-comparison based  methods.

Method 3)  With help of Map.

Algorithm :
1) Create a Map of  <diameter, Address of Bolt>
2) For all N nuts.
check diameter and search in map.
if diameter matches =   declare them both as pair
else print “no bolt for this nut”
3) end

With this simple algo you can find all pairs of nuts and bot in N comparisons only.

Similar Articles

Filed Under: Adobe Interview Questions, Interview Questions, problem Tagged With: Array, Hashmap

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Level order traversal in Spiral form

Find Pythagorean Triplets in an array in O(N)

HackeEarth Flipkart’s Drone

Handle duplicates in Binary Search Tree

Best Java Book | Top Java Programming Book for Beginners

Flipkart SDET Interview Experience

Given a string, find the first character which is non-repetitive

Reliance Jio Software Developer Interview Experience

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Find the kth number with prime factors 3, 5 and 7

Find the element that appears once others appears thrice

Reversal of LinkedList

Python Array String

Find min element in Sorted Rotated Array (Without Duplicates)

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

Spanning Tree

Stock Buy Sell to Maximize Profit

Find position of the only set bit

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

Leetcode: Merge Intervals

Find if two rectangles overlap

Sort an array according to the order defined by another array

BFS (Breath First Search)

Cisco Hiring Event 21st – 22nd Feb 2015

Sequence Finder Dynamic Programming

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Possible sizes of bus to carry n groups of friends

Maximum size of square sub matrix with all 1’s in a binary matrix

Copyright © 2025 · Genesis Framework · WordPress · Log in