• 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

Find and print longest consecutive number sequence in a given sequence in O(n)

Generate next palindrome number

Difference between a LinkedList and a Binary Search Tree BST

Find if two rectangles overlap

Printing each word reverse in string

Count Possible Decodings of a given Digit Sequence

Subset Sum Problem Dynamic programming

Implement a generic binary search algorithm for Integer Double String etc

FizzBuzz Solution C C++

K’th Largest Element in BST when modification to BST is not allowed

Binary Tree Isomorphic to each other

Given Set of words or A String find whether chain is possible from these words or not

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

Practo Hiring Experience

DFS (Depth First Search)

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Find if a binary tree is height balanced ?

Connect n ropes with minimum cost

Find the number ABCD such that when multipled by 4 gives DCBA.

Maximum sum contiguous subarray of an Array

Search element in a matrix with all rows and columns in sorted order

Maximum path sum between two leaves

C++ OOPs Part2

Flipkart SDET Interview Experience

Check Binary Tree is Binary Search Tree or not

Print Power Set of a Set

Skiing on Mountains Matrix

Sort an array according to the order defined by another array

Leetcode: Edit Distance

Print all nodes that are at distance k from a leaf node

Copyright © 2025 · Genesis Framework · WordPress · Log in